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