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