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