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