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