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