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