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