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