Apply console part of patch [#26044] Windows thumbnails and improved filetype registr...
[blender.git] / source / blender / windowmanager / intern / wm_init_exit.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
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. 
10  *
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.
15  *
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.
19  *
20  * The Original Code is Copyright (C) 2007 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \file blender/windowmanager/intern/wm_init_exit.c
30  *  \ingroup wm
31  */
32
33
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37
38 #include "MEM_guardedalloc.h"
39 #include "MEM_CacheLimiterC-Api.h"
40
41 #include "IMB_imbuf_types.h"
42 #include "IMB_imbuf.h"
43
44 #include "DNA_object_types.h"
45 #include "DNA_scene_types.h"
46 #include "DNA_userdef_types.h"
47 #include "DNA_windowmanager_types.h"
48
49 #include "BKE_blender.h"
50 #include "BKE_context.h"
51 #include "BKE_curve.h"
52 #include "BKE_displist.h"
53 #include "BKE_DerivedMesh.h"
54 #include "BKE_font.h"
55 #include "BKE_global.h"
56 #include "BKE_library.h"
57 #include "BKE_main.h"
58 #include "BKE_mball.h"
59 #include "BKE_report.h"
60
61 #include "BKE_packedFile.h"
62 #include "BKE_sequencer.h" /* free seq clipboard */
63 #include "BKE_material.h" /* clear_matcopybuf */
64
65 #include "BLI_blenlib.h"
66 #include "BLI_winstuff.h"
67
68 #include "RE_pipeline.h"                /* RE_ free stuff */
69
70 #ifdef WITH_PYTHON
71 #include "BPY_extern.h"
72 #endif
73
74 #ifdef WITH_GAMEENGINE
75 #include "SYS_System.h"
76 #endif
77 #include "GHOST_Path-api.h"
78
79 #include "RNA_define.h"
80
81 #include "WM_api.h"
82 #include "WM_types.h"
83
84 #include "wm_cursors.h"
85 #include "wm_event_system.h"
86 #include "wm.h"
87 #include "wm_files.h"
88 #include "wm_window.h"
89
90 #include "ED_armature.h"
91 #include "ED_keyframing.h"
92 #include "ED_node.h"
93 #include "ED_render.h"
94 #include "ED_space_api.h"
95 #include "ED_screen.h"
96 #include "ED_util.h"
97
98 #include "UI_interface.h"
99 #include "BLF_api.h"
100
101 #include "GPU_buffers.h"
102 #include "GPU_extensions.h"
103 #include "GPU_draw.h"
104
105 #include "BKE_depsgraph.h"
106 #include "BKE_sound.h"
107 #include "GHOST_C-api.h"
108
109 static void wm_init_reports(bContext *C)
110 {
111         BKE_reports_init(CTX_wm_reports(C), RPT_STORE);
112 }
113 static void wm_free_reports(bContext *C)
114 {
115         BKE_reports_clear(CTX_wm_reports(C));
116 }
117
118 int wm_start_with_console = 0;
119
120 /* only called once, for startup */
121 void WM_init(bContext *C, int argc, const char **argv)
122 {
123
124         if (!G.background) {
125                 wm_ghost_init(C);       /* note: it assigns C to ghost! */
126                 wm_init_cursor_data();
127 #ifdef WIN32
128                 if (IsConsoleEmpty()) /* never hide if the console window pre-existed */
129                         WM_console_toggle(C, wm_start_with_console);
130 #endif
131         }
132         GHOST_CreateSystemPaths();
133
134         wm_operatortype_init();
135         
136         set_free_windowmanager_cb(wm_close_and_free);   /* library.c */
137         set_blender_test_break_cb(wm_window_testbreak); /* blender.c */
138         DAG_editors_update_cb(ED_render_id_flush_update); /* depsgraph.c */
139         
140         ED_spacetypes_init();   /* editors/space_api/spacetype.c */
141         
142         ED_file_init();                 /* for fsmenu */
143         ED_init_node_butfuncs();        
144         
145         BLF_init(11, U.dpi); /* Please update source/gamengine/GamePlayer/GPG_ghost.cpp if you change this */
146         BLF_lang_init();
147         
148         /* get the default database, plus a wm */
149         WM_read_homefile(C, NULL, G.factory_startup);
150
151         /* note: there is a bug where python needs initializing before loading the
152          * startup.blend because it may contain PyDrivers. It also needs to be after
153          * initializing space types and other internal data.
154          *
155          * However cant redo this at the moment. Solution is to load python
156          * before WM_read_homefile() or make py-drivers check if python is running.
157          * Will try fix when the crash can be repeated. - campbell. */
158
159 #ifdef WITH_PYTHON
160         BPY_context_set(C); /* necessary evil */
161         BPY_python_start(argc, argv);
162
163         BPY_driver_reset();
164         BPY_modules_load_user(C);
165 #else
166         (void)argc; /* unused */
167         (void)argv; /* unused */
168 #endif
169
170         wm_init_reports(C); /* reports cant be initialized before the wm */
171
172         if (!G.background) {
173                 GPU_extensions_init();
174                 GPU_set_mipmap(!(U.gameflags & USER_DISABLE_MIPMAP));
175         
176                 UI_init();
177         }
178         
179         clear_matcopybuf();
180         ED_render_clear_mtex_copybuf();
181
182         //      glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
183                 
184         ED_preview_init_dbase();
185         
186         G.ndofdevice = -1;      /* XXX bad initializer, needs set otherwise buttons show! */
187         
188         WM_read_history();
189
190         /* allow a path of "", this is what happens when making a new file */
191         /*
192         if(G.main->name[0] == 0)
193                 BLI_make_file_string("/", G.main->name, BLI_getDefaultDocumentFolder(), "untitled.blend");
194         */
195
196         BLI_strncpy(G.lib, G.main->name, FILE_MAX);
197
198 }
199
200 void WM_init_splash(bContext *C)
201 {
202         if((U.uiflag & USER_SPLASH_DISABLE) == 0) {
203                 wmWindowManager *wm= CTX_wm_manager(C);
204                 wmWindow *prevwin= CTX_wm_window(C);
205         
206                 if(wm->windows.first) {
207                         CTX_wm_window_set(C, wm->windows.first);
208                         WM_operator_name_call(C, "WM_OT_splash", WM_OP_INVOKE_DEFAULT, NULL);
209                         CTX_wm_window_set(C, prevwin);
210                 }
211         }
212 }
213
214 static ScrArea *biggest_view3d(bContext *C)
215 {
216         bScreen *sc= CTX_wm_screen(C);
217         ScrArea *sa, *big= NULL;
218         int size, maxsize= 0;
219
220         for(sa= sc->areabase.first; sa; sa= sa->next) {
221                 if(sa->spacetype==SPACE_VIEW3D) {
222                         size= sa->winx * sa->winy;
223                         if(size > maxsize) {
224                                 maxsize= size;
225                                 big= sa;
226                         }
227                 }
228         }
229         return big;
230 }
231
232 int WM_init_game(bContext *C)
233 {
234         wmWindowManager *wm= CTX_wm_manager(C);
235         wmWindow* win;
236
237         ScrArea *sa;
238         ARegion *ar= NULL;
239
240         Scene *scene= CTX_data_scene(C);
241
242         if (!scene) {
243                 // XXX, this should not be needed.
244                 Main *bmain = CTX_data_main(C);
245                 scene= bmain->scene.first;
246         }
247
248         win = wm->windows.first;
249
250         //first to get a valid window
251         if(win)
252                 CTX_wm_window_set(C, win);
253
254         sa = biggest_view3d(C);
255
256         if(sa)
257         {
258                 for(ar=sa->regionbase.first; ar; ar=ar->next) {
259                         if(ar->regiontype == RGN_TYPE_WINDOW) {
260                                 break;
261                         }
262                 }
263         }
264
265         // if we have a valid 3D view
266         if (sa && ar) {
267                 ARegion *arhide;
268
269                 CTX_wm_area_set(C, sa);
270                 CTX_wm_region_set(C, ar);
271
272                 /* disable quad view */
273                 if(ar->alignment == RGN_ALIGN_QSPLIT)
274                         WM_operator_name_call(C, "SCREEN_OT_region_quadview", WM_OP_EXEC_DEFAULT, NULL);
275
276                 /* toolbox, properties panel and header are hidden */
277                 for(arhide=sa->regionbase.first; arhide; arhide=arhide->next) {
278                         if(arhide->regiontype != RGN_TYPE_WINDOW) {
279                                 if(!(arhide->flag & RGN_FLAG_HIDDEN)) {
280                                         ED_region_toggle_hidden(C, arhide);
281                                 }
282                         }
283                 }
284
285                 /* full screen the area */
286                 if(!sa->full) {
287                         ED_screen_full_toggle(C, win, sa);
288                 }
289
290                 /* Fullscreen */
291                 if(scene->gm.fullscreen) {
292                         WM_operator_name_call(C, "WM_OT_window_fullscreen_toggle", WM_OP_EXEC_DEFAULT, NULL);
293                         wm_get_screensize(&ar->winrct.xmax, &ar->winrct.ymax);
294                         ar->winx = ar->winrct.xmax + 1;
295                         ar->winy = ar->winrct.ymax + 1;
296                 }
297                 else
298                 {
299                         GHOST_RectangleHandle rect = GHOST_GetClientBounds(win->ghostwin);
300                         ar->winrct.ymax = GHOST_GetHeightRectangle(rect);
301                         ar->winrct.xmax = GHOST_GetWidthRectangle(rect);
302                         ar->winx = ar->winrct.xmax + 1;
303                         ar->winy = ar->winrct.ymax + 1;
304                         GHOST_DisposeRectangle(rect);
305                 }
306
307                 WM_operator_name_call(C, "VIEW3D_OT_game_start", WM_OP_EXEC_DEFAULT, NULL);
308
309                 return 1;
310         }
311         else
312         {
313                 ReportTimerInfo *rti;
314
315                 BKE_report(&wm->reports, RPT_ERROR, "No valid 3D View found. Game auto start is not possible.");
316
317                 /* After adding the report to the global list, reset the report timer. */
318                 WM_event_remove_timer(wm, NULL, wm->reports.reporttimer);
319
320                 /* Records time since last report was added */
321                 wm->reports.reporttimer = WM_event_add_timer(wm, CTX_wm_window(C), TIMER, 0.02);
322
323                 rti = MEM_callocN(sizeof(ReportTimerInfo), "ReportTimerInfo");
324                 wm->reports.reporttimer->customdata = rti;
325         }
326         return 0;
327 }
328
329 /* free strings of open recent files */
330 static void free_openrecent(void)
331 {
332         struct RecentFile *recent;
333         
334         for(recent = G.recent_files.first; recent; recent=recent->next)
335                 MEM_freeN(recent->filepath);
336         
337         BLI_freelistN(&(G.recent_files));
338 }
339
340
341 /* bad stuff*/
342
343 extern ListBase editelems;
344 extern wchar_t *copybuf;
345 extern wchar_t *copybufinfo;
346
347         // XXX copy/paste buffer stuff...
348 extern void free_anim_copybuf(void); 
349 extern void free_anim_drivers_copybuf(void); 
350 extern void free_fmodifiers_copybuf(void); 
351 extern void free_posebuf(void); 
352
353 /* called in creator.c even... tsk, split this! */
354 void WM_exit(bContext *C)
355 {
356         wmWindow *win;
357
358         sound_exit();
359
360         WM_console_toggle(C, 1); /* never leave behind invisible consoles */
361
362         /* first wrap up running stuff, we assume only the active WM is running */
363         /* modal handlers are on window level freed, others too? */
364         /* note; same code copied in wm_files.c */
365         if(C && CTX_wm_manager(C)) {
366                 
367                 WM_jobs_stop_all(CTX_wm_manager(C));
368                 
369                 for(win= CTX_wm_manager(C)->windows.first; win; win= win->next) {
370                         
371                         CTX_wm_window_set(C, win);      /* needed by operator close callbacks */
372                         WM_event_remove_handlers(C, &win->handlers);
373                         WM_event_remove_handlers(C, &win->modalhandlers);
374                         ED_screen_exit(C, win, win->screen);
375                 }
376         }
377         wm_operatortype_free();
378         wm_dropbox_free();
379         WM_menutype_free();
380         
381         /* all non-screen and non-space stuff editors did, like editmode */
382         if(C)
383                 ED_editors_exit(C);
384
385 //      XXX     
386 //      BIF_GlobalReebFree();
387 //      BIF_freeRetarget();
388         BIF_freeTemplates(C);
389         
390         free_ttfont(); /* bke_font.h */
391         
392         free_openrecent();
393         
394         BKE_freecubetable();
395         
396         fastshade_free_render();        /* shaded view */
397         ED_preview_free_dbase();        /* frees a Main dbase, before free_blender! */
398
399         if(C && CTX_wm_manager(C))
400                 wm_free_reports(C);                     /* before free_blender! - since the ListBases get freed there */
401
402         seq_free_clipboard(); /* sequencer.c */
403                 
404         free_blender();                         /* blender.c, does entire library and spacetypes */
405 //      free_matcopybuf();
406         free_anim_copybuf();
407         free_anim_drivers_copybuf();
408         free_fmodifiers_copybuf();
409         free_posebuf();
410 //      free_vertexpaint();
411 //      free_imagepaint();
412         
413 //      fsmenu_free();
414
415         BLF_exit();
416         
417         ANIM_keyingset_infos_exit();
418         
419         RE_FreeAllRender();
420         RE_engines_exit();
421         
422 //      free_txt_data();
423         
424
425 #ifdef WITH_PYTHON
426         /* XXX - old note */
427         /* before free_blender so py's gc happens while library still exists */
428         /* needed at least for a rare sigsegv that can happen in pydrivers */
429
430         /* Update for blender 2.5, move after free_blender because blender now holds references to PyObject's
431          * so decref'ing them after python ends causes bad problems every time
432          * the pyDriver bug can be fixed if it happens again we can deal with it then */
433         BPY_python_end();
434 #endif
435
436         if (!G.background) {
437 // XXX          UI_filelist_free_icons();
438         }
439         
440         GPU_buffer_pool_free(NULL);
441         GPU_free_unused_buffers();
442         GPU_extensions_exit();
443         
444 //      if (copybuf) MEM_freeN(copybuf);
445 //      if (copybufinfo) MEM_freeN(copybufinfo);
446         if (!G.background) {
447                 BKE_undo_save_quit();   // saves quit.blend if global undo is on
448         }
449         BKE_reset_undo(); 
450         
451         ED_file_exit(); /* for fsmenu */
452
453         UI_exit();
454         BKE_userdef_free();
455
456         RNA_exit(); /* should be after BPY_python_end so struct python slots are cleared */
457         
458         wm_ghost_exit();
459
460         CTX_free(C);
461 #ifdef WITH_GAMEENGINE
462         SYS_DeleteSystem(SYS_GetSystem());
463 #endif
464         
465         GHOST_DisposeSystemPaths();
466
467         if(MEM_get_memory_blocks_in_use()!=0) {
468                 printf("Error: Not freed memory blocks: %d\n", MEM_get_memory_blocks_in_use());
469                 MEM_printmemlist();
470         }
471         wm_autosave_delete();
472         
473         printf("\nBlender quit\n");
474         
475 #ifdef WIN32   
476         /* ask user to press enter when in debug mode */
477         if(G.f & G_DEBUG) {
478                 printf("press enter key to exit...\n\n");
479                 getchar();
480         }
481 #endif 
482         
483         exit(G.afbreek==1);
484 }
485