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