4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * The Original Code is: all of this file.
25 * Contributor(s): none yet.
27 * ***** END GPL LICENSE BLOCK *****
30 /** \file blender/blenkernel/intern/blender.c
36 #include <unistd.h> // for read close
38 #include <io.h> // for open close read
49 #include <fcntl.h> // for open
51 #include "MEM_guardedalloc.h"
53 #include "DNA_userdef_types.h"
54 #include "DNA_scene_types.h"
55 #include "DNA_screen_types.h"
56 #include "DNA_sequence_types.h"
57 #include "DNA_sound_types.h"
59 #include "BLI_blenlib.h"
60 #include "BLI_bpath.h"
61 #include "BLI_dynstr.h"
62 #include "BLI_path_util.h"
63 #include "BLI_utildefines.h"
64 #include "BLI_callbacks.h"
66 #include "IMB_imbuf.h"
68 #include "BKE_blender.h"
69 #include "BKE_context.h"
70 #include "BKE_depsgraph.h"
71 #include "BKE_displist.h"
72 #include "BKE_global.h"
73 #include "BKE_idprop.h"
75 #include "BKE_library.h"
78 #include "BKE_report.h"
79 #include "BKE_scene.h"
80 #include "BKE_screen.h"
81 #include "BKE_sequencer.h"
82 #include "BKE_sound.h"
85 #include "BLO_undofile.h"
86 #include "BLO_readfile.h"
87 #include "BLO_writefile.h"
89 #include "BKE_utildefines.h"
91 #include "RNA_access.h"
93 #include "WM_api.h" // XXXXX BAD, very BAD dependency (bad level call) - remove asap, elubie
97 /* ListBase = {NULL, NULL}; */
99 char versionstr[48]= "";
101 /* ********** free ********** */
103 /* only to be called on exit blender */
104 void free_blender(void)
106 /* samples are in a global list..., also sets G.main->sound->sample NULL */
110 BKE_spacetypes_free(); /* after free main, it uses space callbacks */
116 seq_stripelem_cache_destruct();
121 void initglobals(void)
123 memset(&G, 0, sizeof(Global));
127 G.main= MEM_callocN(sizeof(Main), "initglobals");
131 if(BLENDER_SUBVERSION)
132 BLI_snprintf(versionstr, sizeof(versionstr), "blender.org %d.%d", BLENDER_VERSION, BLENDER_SUBVERSION);
134 BLI_snprintf(versionstr, sizeof(versionstr), "blender.org %d", BLENDER_VERSION);
136 #ifdef _WIN32 // FULLSCREEN
137 G.windowstate = G_WINDOWSTATE_USERDEF;
140 G.charstart = 0x0000;
144 #ifndef WITH_PYTHON_SECURITY /* default */
145 G.f |= G_SCRIPT_AUTOEXEC;
147 G.f &= ~G_SCRIPT_AUTOEXEC;
153 static void clear_global(void)
155 // extern short winqueue_break; /* screen.c */
157 free_main(G.main); /* free all lib data */
159 // free_vertexpaint();
164 /* make sure path names are correct for OS */
165 static void clean_paths(Main *main)
167 struct BPathIterator *bpi;
168 char filepath_expanded[1024];
171 for(BLI_bpathIterator_init(&bpi, main, main->name, BPATH_USE_PACKED); !BLI_bpathIterator_isDone(bpi); BLI_bpathIterator_step(bpi)) {
172 BLI_bpathIterator_getPath(bpi, filepath_expanded);
174 BLI_clean(filepath_expanded);
176 BLI_bpathIterator_setPath(bpi, filepath_expanded);
179 BLI_bpathIterator_free(bpi);
181 for(scene= main->scene.first; scene; scene= scene->id.next) {
182 BLI_clean(scene->r.pic);
186 /* context matching */
187 /* handle no-ui case */
189 /* note, this is called on Undo so any slow conversion functions here
190 * should be avoided or check (mode!='u') */
192 static void setup_app_data(bContext *C, BlendFileData *bfd, const char *filepath)
194 bScreen *curscreen= NULL;
195 Scene *curscene= NULL;
199 /* 'u' = undo save, 'n' = no UI load */
200 if(bfd->main->screen.first==NULL) mode= 'u';
201 else if(G.fileflags & G_FILE_NO_UI) mode= 'n';
204 recover= (G.fileflags & G_FILE_RECOVER);
206 /* Only make filepaths compatible when loading for real (not undo) */
208 clean_paths(bfd->main);
211 /* XXX here the complex windowmanager matching */
213 /* no load screens? */
215 /* comes from readfile.c */
216 extern void lib_link_screen_restore(Main *, bScreen *, Scene *);
218 SWAP(ListBase, G.main->wm, bfd->main->wm);
219 SWAP(ListBase, G.main->screen, bfd->main->screen);
220 SWAP(ListBase, G.main->script, bfd->main->script);
222 /* we re-use current screen */
223 curscreen= CTX_wm_screen(C);
224 /* but use new Scene pointer */
225 curscene= bfd->curscene;
226 if(curscene==NULL) curscene= bfd->main->scene.first;
227 /* and we enforce curscene to be in current screen */
228 if(curscreen) curscreen->scene= curscene; /* can run in bgmode */
230 /* clear_global will free G.main, here we can still restore pointers */
231 lib_link_screen_restore(bfd->main, curscreen, curscene);
234 /* free G.main Main database */
235 // CTX_wm_manager_set(C, NULL);
238 /* clear old property update cache, in case some old references are left dangling */
239 RNA_property_update_cache_free();
243 CTX_data_main_set(C, G.main);
245 sound_init_main(G.main);
249 /* only here free userdef themes... */
253 MEM_freeN(bfd->user);
256 /* case G_FILE_NO_UI or no screens in file */
258 /* leave entire context further unaltered? */
259 CTX_data_scene_set(C, curscene);
262 G.winpos= bfd->winpos;
263 G.displaymode= bfd->displaymode;
264 G.fileflags= bfd->fileflags;
265 CTX_wm_manager_set(C, bfd->main->wm.first);
266 CTX_wm_screen_set(C, bfd->curscreen);
267 CTX_data_scene_set(C, bfd->curscreen->scene);
268 CTX_wm_area_set(C, NULL);
269 CTX_wm_region_set(C, NULL);
270 CTX_wm_menu_set(C, NULL);
273 /* this can happen when active scene was lib-linked, and doesnt exist anymore */
274 if(CTX_data_scene(C)==NULL) {
275 CTX_data_scene_set(C, bfd->main->scene.first);
276 CTX_wm_screen(C)->scene= CTX_data_scene(C);
277 curscene= CTX_data_scene(C);
280 /* special cases, override loaded flags: */
281 if(G.f != bfd->globalf) {
282 const int flags_keep= (G_DEBUG | G_SWAP_EXCHANGE | G_SCRIPT_AUTOEXEC | G_SCRIPT_OVERRIDE_PREF);
283 bfd->globalf= (bfd->globalf & ~flags_keep) | (G.f & flags_keep);
290 //setscreen(G.curscreen);
293 // FIXME: this version patching should really be part of the file-reading code,
294 // but we still get too many unrelated data-corruption crashes otherwise...
295 if (G.main->versionfile < 250)
296 do_versions_ipos_to_animato(G.main);
298 if(recover && bfd->filename[0] && G.relbase_valid) {
299 /* in case of autosave or quit.blend, use original filename instead
300 * use relbase_valid to make sure the file is saved, else we get <memory2> in the filename */
301 filepath= bfd->filename;
304 else if (!G.relbase_valid) {
305 /* otherwise, use an empty string as filename, rather than <memory2> */
310 /* these are the same at times, should never copy to the same location */
311 if(G.main->name != filepath)
312 BLI_strncpy(G.main->name, filepath, FILE_MAX);
314 /* baseflags, groups, make depsgraph, etc */
315 set_scene_bg(G.main, CTX_data_scene(C));
319 (void)curscene; /* quiet warning */
322 static int handle_subversion_warning(Main *main, ReportList *reports)
324 if(main->minversionfile > BLENDER_VERSION ||
325 (main->minversionfile == BLENDER_VERSION &&
326 main->minsubversionfile > BLENDER_SUBVERSION)) {
327 BKE_reportf(reports, RPT_ERROR, "File written by newer Blender binary: %d.%d , expect loss of data!", main->minversionfile, main->minsubversionfile);
333 static void keymap_item_free(wmKeyMapItem *kmi)
335 if(kmi->properties) {
336 IDP_FreeProperty(kmi->properties);
337 MEM_freeN(kmi->properties);
343 void BKE_userdef_free(void)
347 wmKeyMapDiffItem *kmdi;
349 for(km=U.user_keymaps.first; km; km=km->next) {
350 for(kmdi=km->diff_items.first; kmdi; kmdi=kmdi->next) {
352 keymap_item_free(kmdi->add_item);
353 MEM_freeN(kmdi->add_item);
355 if(kmdi->remove_item) {
356 keymap_item_free(kmdi->remove_item);
357 MEM_freeN(kmdi->remove_item);
361 for(kmi=km->items.first; kmi; kmi=kmi->next)
362 keymap_item_free(kmi);
364 BLI_freelistN(&km->diff_items);
365 BLI_freelistN(&km->items);
368 BLI_freelistN(&U.uistyles);
369 BLI_freelistN(&U.uifonts);
370 BLI_freelistN(&U.themes);
371 BLI_freelistN(&U.user_keymaps);
372 BLI_freelistN(&U.addons);
375 int BKE_read_file(bContext *C, const char *filepath, ReportList *reports)
378 int retval= BKE_READ_FILE_OK;
380 if(strstr(filepath, BLENDER_STARTUP_FILE)==NULL) /* dont print user-pref loading */
381 printf("read blend: %s\n", filepath);
383 bfd= BLO_read_from_file(filepath, reports);
385 if(bfd->user) retval= BKE_READ_FILE_OK_USERPREFS;
387 if(0==handle_subversion_warning(bfd->main, reports)) {
388 free_main(bfd->main);
391 retval= BKE_READ_FILE_FAIL;
394 setup_app_data(C, bfd, filepath); // frees BFD
397 BKE_reports_prependf(reports, "Loading %s failed: ", filepath);
399 return (bfd?retval:BKE_READ_FILE_FAIL);
402 int BKE_read_file_from_memory(bContext *C, char* filebuf, int filelength, ReportList *reports)
406 bfd= BLO_read_from_memory(filebuf, filelength, reports);
408 setup_app_data(C, bfd, "<memory2>");
410 BKE_reports_prepend(reports, "Loading failed: ");
415 /* memfile is the undo buffer */
416 int BKE_read_file_from_memfile(bContext *C, MemFile *memfile, ReportList *reports)
420 bfd= BLO_read_from_memfile(CTX_data_main(C), G.main->name, memfile, reports);
422 setup_app_data(C, bfd, "<memory1>");
424 BKE_reports_prepend(reports, "Loading failed: ");
430 /* ***************** testing for break ************* */
432 static void (*blender_test_break_cb)(void)= NULL;
434 void set_blender_test_break_cb(void (*func)(void) )
436 blender_test_break_cb= func;
440 int blender_test_break(void)
443 if (blender_test_break_cb)
444 blender_test_break_cb();
447 return (G.afbreek==1);
451 /* ***************** GLOBAL UNDO *************** */
455 #define MAXUNDONAME 64
456 typedef struct UndoElem {
457 struct UndoElem *next, *prev;
458 char str[FILE_MAXDIR+FILE_MAXFILE];
459 char name[MAXUNDONAME];
464 static ListBase undobase={NULL, NULL};
465 static UndoElem *curundo= NULL;
468 static int read_undosave(bContext *C, UndoElem *uel)
470 char mainstr[sizeof(G.main->name)];
471 int success=0, fileflags;
473 /* This is needed so undoing/redoing doesnt crash with threaded previews going */
474 WM_jobs_stop_all(CTX_wm_manager(C));
476 BLI_strncpy(mainstr, G.main->name, sizeof(mainstr)); /* temporal store */
478 fileflags= G.fileflags;
479 G.fileflags |= G_FILE_NO_UI;
482 success= (BKE_read_file(C, uel->str, NULL) != BKE_READ_FILE_FAIL);
484 success= BKE_read_file_from_memfile(C, &uel->memfile, NULL);
487 BLI_strncpy(G.main->name, mainstr, sizeof(G.main->name)); /* restore */
488 G.fileflags= fileflags;
491 /* important not to update time here, else non keyed tranforms are lost */
492 DAG_on_visible_update(G.main, FALSE);
498 /* name can be a dynamic string */
499 void BKE_write_undo(bContext *C, const char *name)
501 uintptr_t maxmem, totmem, memused;
502 int nr /*, success */ /* UNUSED */;
505 if( (U.uiflag & USER_GLOBALUNDO)==0) return;
506 if( U.undosteps==0) return;
508 /* remove all undos after (also when curundo==NULL) */
509 while(undobase.last != curundo) {
511 BLI_remlink(&undobase, uel);
512 BLO_free_memfile(&uel->memfile);
517 curundo= uel= MEM_callocN(sizeof(UndoElem), "undo file");
518 BLI_strncpy(uel->name, name, sizeof(uel->name));
519 BLI_addtail(&undobase, uel);
521 /* and limit amount to the maximum */
526 if(nr==U.undosteps) break;
530 while(undobase.first!=uel) {
531 UndoElem *first= undobase.first;
532 BLI_remlink(&undobase, first);
533 /* the merge is because of compression */
534 BLO_merge_memfile(&first->memfile, &first->next->memfile);
540 /* disk save version */
542 static int counter= 0;
543 char filepath[FILE_MAXDIR+FILE_MAXFILE];
545 int fileflags = G.fileflags & ~(G_FILE_HISTORY); /* don't do file history on undo */
547 /* calculate current filepath */
549 counter= counter % U.undosteps;
551 BLI_snprintf(numstr, sizeof(numstr), "%d.blend", counter);
552 BLI_make_file_string("/", filepath, BLI_temporary_dir(), numstr);
554 /* success= */ /* UNUSED */ BLO_write_file(CTX_data_main(C), filepath, fileflags, NULL, NULL);
556 BLI_strncpy(curundo->str, filepath, sizeof(curundo->str));
559 MemFile *prevfile=NULL;
561 if(curundo->prev) prevfile= &(curundo->prev->memfile);
563 memused= MEM_get_memory_in_use();
564 /* success= */ /* UNUSED */ BLO_write_file_mem(CTX_data_main(C), prevfile, &curundo->memfile, G.fileflags);
565 curundo->undosize= MEM_get_memory_in_use() - memused;
568 if(U.undomemory != 0) {
569 /* limit to maximum memory (afterwards, we can't know in advance) */
571 maxmem= ((uintptr_t)U.undomemory)*1024*1024;
573 /* keep at least two (original + other) */
575 while(uel && uel->prev) {
576 totmem+= uel->undosize;
577 if(totmem>maxmem) break;
582 if(uel->prev && uel->prev->prev)
585 while(undobase.first!=uel) {
586 UndoElem *first= undobase.first;
587 BLI_remlink(&undobase, first);
588 /* the merge is because of compression */
589 BLO_merge_memfile(&first->memfile, &first->next->memfile);
596 /* 1= an undo, -1 is a redo. we have to make sure 'curundo' remains at current situation */
597 void BKE_undo_step(bContext *C, int step)
601 read_undosave(C, curundo);
604 /* curundo should never be NULL, after restart or load file it should call undo_save */
605 if(curundo==NULL || curundo->prev==NULL) ; // XXX error("No undo available");
607 if(G.f & G_DEBUG) printf("undo %s\n", curundo->name);
608 curundo= curundo->prev;
609 read_undosave(C, curundo);
614 /* curundo has to remain current situation! */
616 if(curundo==NULL || curundo->next==NULL) ; // XXX error("No redo available");
618 read_undosave(C, curundo->next);
619 curundo= curundo->next;
620 if(G.f & G_DEBUG) printf("redo %s\n", curundo->name);
625 void BKE_reset_undo(void)
631 BLO_free_memfile(&uel->memfile);
635 BLI_freelistN(&undobase);
639 /* based on index nr it does a restore */
640 void BKE_undo_number(bContext *C, int nr)
642 curundo= BLI_findlink(&undobase, nr);
646 /* go back to the last occurance of name in stack */
647 void BKE_undo_name(bContext *C, const char *name)
649 UndoElem *uel= BLI_rfindstring(&undobase, name, offsetof(UndoElem, name));
651 if(uel && uel->prev) {
658 int BKE_undo_valid(const char *name)
661 UndoElem *uel= BLI_rfindstring(&undobase, name, offsetof(UndoElem, name));
662 return uel && uel->prev;
665 return undobase.last != undobase.first;
668 /* get name of undo item, return null if no item with this index */
669 /* if active pointer, set it to 1 if true */
670 char *BKE_undo_get_name(int nr, int *active)
672 UndoElem *uel= BLI_findlink(&undobase, nr);
674 if(active) *active= 0;
677 if(active && uel==curundo)
684 char *BKE_undo_menu_string(void)
687 DynStr *ds= BLI_dynstr_new();
690 BLI_dynstr_append(ds, "Global Undo History %t");
692 for(uel= undobase.first; uel; uel= uel->next) {
693 BLI_dynstr_append(ds, "|");
694 BLI_dynstr_append(ds, uel->name);
697 menu= BLI_dynstr_get_cstring(ds);
703 /* saves quit.blend */
704 void BKE_undo_save_quit(void)
709 char str[FILE_MAXDIR+FILE_MAXFILE];
711 if( (U.uiflag & USER_GLOBALUNDO)==0) return;
715 printf("No undo buffer to save recovery file\n");
719 /* no undo state to save */
720 if(undobase.first==undobase.last) return;
722 BLI_make_file_string("/", str, BLI_temporary_dir(), "quit.blend");
724 file = open(str,O_BINARY+O_WRONLY+O_CREAT+O_TRUNC, 0666);
726 //XXX error("Unable to save %s, check you have permissions", str);
730 chunk= uel->memfile.chunks.first;
732 if( write(file, chunk->buf, chunk->size) != chunk->size) break;
738 if(chunk) ; //XXX error("Unable to save %s, internal error", str);
739 else printf("Saved session recovery to %s\n", str);
743 Main *BKE_undo_get_main(Scene **scene)
746 BlendFileData *bfd= BLO_read_from_memfile(G.main, G.main->name, &curundo->memfile, NULL);
751 *scene= bfd->curscene;