1 /* blender.c jan 94 MIXED MODEL
3 * common help functions and data
7 * ***** BEGIN GPL LICENSE BLOCK *****
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24 * All rights reserved.
26 * The Original Code is: all of this file.
28 * Contributor(s): none yet.
30 * ***** END GPL LICENSE BLOCK *****
33 /** \file blender/blenkernel/intern/blender.c
39 #include <unistd.h> // for read close
41 #include <io.h> // for open close read
52 #include <fcntl.h> // for open
54 #include "MEM_guardedalloc.h"
56 #include "DNA_userdef_types.h"
57 #include "DNA_scene_types.h"
58 #include "DNA_screen_types.h"
59 #include "DNA_sequence_types.h"
60 #include "DNA_sound_types.h"
62 #include "BLI_blenlib.h"
63 #include "BLI_bpath.h"
64 #include "BLI_dynstr.h"
65 #include "BLI_path_util.h"
66 #include "BLI_utildefines.h"
67 #include "BLI_callbacks.h"
69 #include "IMB_imbuf.h"
71 #include "BKE_blender.h"
72 #include "BKE_context.h"
73 #include "BKE_depsgraph.h"
74 #include "BKE_displist.h"
75 #include "BKE_global.h"
76 #include "BKE_idprop.h"
78 #include "BKE_library.h"
81 #include "BKE_report.h"
82 #include "BKE_scene.h"
83 #include "BKE_screen.h"
84 #include "BKE_sequencer.h"
87 #include "BLO_undofile.h"
88 #include "BLO_readfile.h"
89 #include "BLO_writefile.h"
91 #include "BKE_utildefines.h"
93 #include "WM_api.h" // XXXXX BAD, very BAD dependency (bad level call) - remove asap, elubie
97 /* ListBase = {NULL, NULL}; */
100 static char versionstr[48]= "";
102 /* ********** free ********** */
104 /* only to be called on exit blender */
105 void free_blender(void)
107 /* samples are in a global list..., also sets G.main->sound->sample NULL */
111 BKE_spacetypes_free(); /* after free main, it uses space callbacks */
117 seq_stripelem_cache_destruct();
122 void initglobals(void)
124 memset(&G, 0, sizeof(Global));
128 G.main= MEM_callocN(sizeof(Main), "initglobals");
133 ENDIAN_ORDER= (((char*)&ENDIAN_ORDER)[0])? L_ENDIAN: B_ENDIAN;
135 if(BLENDER_SUBVERSION)
136 BLI_snprintf(versionstr, sizeof(versionstr), "www.blender.org %d.%d", BLENDER_VERSION, BLENDER_SUBVERSION);
138 BLI_snprintf(versionstr, sizeof(versionstr), "www.blender.org %d", BLENDER_VERSION);
140 #ifdef _WIN32 // FULLSCREEN
141 G.windowstate = G_WINDOWSTATE_USERDEF;
144 G.charstart = 0x0000;
148 #ifndef WITH_PYTHON_SECURITY /* default */
149 G.f |= G_SCRIPT_AUTOEXEC;
151 G.f &= ~G_SCRIPT_AUTOEXEC;
157 static void clear_global(void)
159 // extern short winqueue_break; /* screen.c */
161 free_main(G.main); /* free all lib data */
163 // free_vertexpaint();
168 /* make sure path names are correct for OS */
169 static void clean_paths(Main *main)
171 struct BPathIterator *bpi;
172 char filepath_expanded[1024];
175 for(BLI_bpathIterator_init(&bpi, main, main->name, BPATH_USE_PACKED); !BLI_bpathIterator_isDone(bpi); BLI_bpathIterator_step(bpi)) {
176 BLI_bpathIterator_getPath(bpi, filepath_expanded);
178 BLI_clean(filepath_expanded);
180 BLI_bpathIterator_setPath(bpi, filepath_expanded);
183 BLI_bpathIterator_free(bpi);
185 for(scene= main->scene.first; scene; scene= scene->id.next) {
186 BLI_clean(scene->r.pic);
190 /* context matching */
191 /* handle no-ui case */
193 /* note, this is called on Undo so any slow conversion functions here
194 * should be avoided or check (mode!='u') */
196 static void setup_app_data(bContext *C, BlendFileData *bfd, const char *filepath)
198 bScreen *curscreen= NULL;
199 Scene *curscene= NULL;
203 /* 'u' = undo save, 'n' = no UI load */
204 if(bfd->main->screen.first==NULL) mode= 'u';
205 else if(G.fileflags & G_FILE_NO_UI) mode= 'n';
208 recover= (G.fileflags & G_FILE_RECOVER);
210 /* Only make filepaths compatible when loading for real (not undo) */
212 clean_paths(bfd->main);
215 /* XXX here the complex windowmanager matching */
217 /* no load screens? */
219 /* comes from readfile.c */
220 extern void lib_link_screen_restore(Main *, bScreen *, Scene *);
222 SWAP(ListBase, G.main->wm, bfd->main->wm);
223 SWAP(ListBase, G.main->screen, bfd->main->screen);
224 SWAP(ListBase, G.main->script, bfd->main->script);
226 /* we re-use current screen */
227 curscreen= CTX_wm_screen(C);
228 /* but use new Scene pointer */
229 curscene= bfd->curscene;
230 if(curscene==NULL) curscene= bfd->main->scene.first;
231 /* and we enforce curscene to be in current screen */
232 if(curscreen) curscreen->scene= curscene; /* can run in bgmode */
234 /* clear_global will free G.main, here we can still restore pointers */
235 lib_link_screen_restore(bfd->main, curscreen, curscene);
238 /* free G.main Main database */
239 // CTX_wm_manager_set(C, NULL);
244 CTX_data_main_set(C, G.main);
248 /* only here free userdef themes... */
252 MEM_freeN(bfd->user);
255 /* case G_FILE_NO_UI or no screens in file */
257 /* leave entire context further unaltered? */
258 CTX_data_scene_set(C, curscene);
261 G.winpos= bfd->winpos;
262 G.displaymode= bfd->displaymode;
263 G.fileflags= bfd->fileflags;
264 CTX_wm_manager_set(C, bfd->main->wm.first);
265 CTX_wm_screen_set(C, bfd->curscreen);
266 CTX_data_scene_set(C, bfd->curscreen->scene);
267 CTX_wm_area_set(C, NULL);
268 CTX_wm_region_set(C, NULL);
269 CTX_wm_menu_set(C, NULL);
272 /* this can happen when active scene was lib-linked, and doesnt exist anymore */
273 if(CTX_data_scene(C)==NULL) {
274 CTX_data_scene_set(C, bfd->main->scene.first);
275 CTX_wm_screen(C)->scene= CTX_data_scene(C);
276 curscene= CTX_data_scene(C);
279 /* special cases, override loaded flags: */
280 if(G.f != bfd->globalf) {
281 const int flags_keep= (G_DEBUG | G_SWAP_EXCHANGE | G_SCRIPT_AUTOEXEC | G_SCRIPT_OVERRIDE_PREF);
282 bfd->globalf= (bfd->globalf & ~flags_keep) | (G.f & flags_keep);
289 //setscreen(G.curscreen);
292 // FIXME: this version patching should really be part of the file-reading code,
293 // but we still get too many unrelated data-corruption crashes otherwise...
294 if (G.main->versionfile < 250)
295 do_versions_ipos_to_animato(G.main);
297 if(recover && bfd->filename[0] && G.relbase_valid) {
298 /* in case of autosave or quit.blend, use original filename instead
299 * use relbase_valid to make sure the file is saved, else we get <memory2> in the filename */
300 filepath= bfd->filename;
303 else if (!G.relbase_valid) {
304 /* otherwise, use an empty string as filename, rather than <memory2> */
309 /* these are the same at times, should never copy to the same location */
310 if(G.main->name != filepath)
311 BLI_strncpy(G.main->name, filepath, FILE_MAX);
313 /* baseflags, groups, make depsgraph, etc */
314 set_scene_bg(G.main, CTX_data_scene(C));
319 static int handle_subversion_warning(Main *main)
321 if(main->minversionfile > BLENDER_VERSION ||
322 (main->minversionfile == BLENDER_VERSION &&
323 main->minsubversionfile > BLENDER_SUBVERSION)) {
327 BLI_snprintf(str, sizeof(str), "File written by newer Blender binary: %d.%d , expect loss of data!", main->minversionfile, main->minsubversionfile);
333 void BKE_userdef_free(void)
338 for(km=U.keymaps.first; km; km=km->next) {
339 for(kmi=km->items.first; kmi; kmi=kmi->next) {
340 if(kmi->properties) {
341 IDP_FreeProperty(kmi->properties);
342 MEM_freeN(kmi->properties);
348 BLI_freelistN(&km->items);
351 BLI_freelistN(&U.uistyles);
352 BLI_freelistN(&U.uifonts);
353 BLI_freelistN(&U.themes);
354 BLI_freelistN(&U.keymaps);
355 BLI_freelistN(&U.addons);
358 int BKE_read_file(bContext *C, const char *filepath, ReportList *reports)
361 int retval= BKE_READ_FILE_OK;
363 if(strstr(filepath, BLENDER_STARTUP_FILE)==NULL) /* dont print user-pref loading */
364 printf("read blend: %s\n", filepath);
366 bfd= BLO_read_from_file(filepath, reports);
368 if(bfd->user) retval= BKE_READ_FILE_OK_USERPREFS;
370 if(0==handle_subversion_warning(bfd->main)) {
371 free_main(bfd->main);
374 retval= BKE_READ_FILE_FAIL;
377 setup_app_data(C, bfd, filepath); // frees BFD
380 BKE_reports_prependf(reports, "Loading %s failed: ", filepath);
382 return (bfd?retval:BKE_READ_FILE_FAIL);
385 int BKE_read_file_from_memory(bContext *C, char* filebuf, int filelength, ReportList *reports)
389 bfd= BLO_read_from_memory(filebuf, filelength, reports);
391 setup_app_data(C, bfd, "<memory2>");
393 BKE_reports_prepend(reports, "Loading failed: ");
398 /* memfile is the undo buffer */
399 int BKE_read_file_from_memfile(bContext *C, MemFile *memfile, ReportList *reports)
403 bfd= BLO_read_from_memfile(CTX_data_main(C), G.main->name, memfile, reports);
405 setup_app_data(C, bfd, "<memory1>");
407 BKE_reports_prepend(reports, "Loading failed: ");
413 /* ***************** testing for break ************* */
415 static void (*blender_test_break_cb)(void)= NULL;
417 void set_blender_test_break_cb(void (*func)(void) )
419 blender_test_break_cb= func;
423 int blender_test_break(void)
426 if (blender_test_break_cb)
427 blender_test_break_cb();
430 return (G.afbreek==1);
434 /* ***************** GLOBAL UNDO *************** */
438 #define MAXUNDONAME 64
439 typedef struct UndoElem {
440 struct UndoElem *next, *prev;
441 char str[FILE_MAXDIR+FILE_MAXFILE];
442 char name[MAXUNDONAME];
447 static ListBase undobase={NULL, NULL};
448 static UndoElem *curundo= NULL;
451 static int read_undosave(bContext *C, UndoElem *uel)
453 char mainstr[sizeof(G.main->name)];
454 int success=0, fileflags;
456 /* This is needed so undoing/redoing doesnt crash with threaded previews going */
457 WM_jobs_stop_all(CTX_wm_manager(C));
459 BLI_strncpy(mainstr, G.main->name, sizeof(mainstr)); /* temporal store */
461 fileflags= G.fileflags;
462 G.fileflags |= G_FILE_NO_UI;
465 success= (BKE_read_file(C, uel->str, NULL) != BKE_READ_FILE_FAIL);
467 success= BKE_read_file_from_memfile(C, &uel->memfile, NULL);
470 BLI_strncpy(G.main->name, mainstr, sizeof(G.main->name)); /* restore */
471 G.fileflags= fileflags;
474 /* important not to update time here, else non keyed tranforms are lost */
475 DAG_on_visible_update(G.main, FALSE);
481 /* name can be a dynamic string */
482 void BKE_write_undo(bContext *C, const char *name)
484 uintptr_t maxmem, totmem, memused;
488 if( (U.uiflag & USER_GLOBALUNDO)==0) return;
489 if( U.undosteps==0) return;
491 /* remove all undos after (also when curundo==NULL) */
492 while(undobase.last != curundo) {
494 BLI_remlink(&undobase, uel);
495 BLO_free_memfile(&uel->memfile);
500 curundo= uel= MEM_callocN(sizeof(UndoElem), "undo file");
501 strncpy(uel->name, name, MAXUNDONAME-1);
502 BLI_addtail(&undobase, uel);
504 /* and limit amount to the maximum */
509 if(nr==U.undosteps) break;
513 while(undobase.first!=uel) {
514 UndoElem *first= undobase.first;
515 BLI_remlink(&undobase, first);
516 /* the merge is because of compression */
517 BLO_merge_memfile(&first->memfile, &first->next->memfile);
523 /* disk save version */
525 static int counter= 0;
526 char filepath[FILE_MAXDIR+FILE_MAXFILE];
528 int fileflags = G.fileflags & ~(G_FILE_HISTORY); /* don't do file history on undo */
530 /* calculate current filepath */
532 counter= counter % U.undosteps;
534 BLI_snprintf(numstr, sizeof(numstr), "%d.blend", counter);
535 BLI_make_file_string("/", filepath, btempdir, numstr);
537 success= BLO_write_file(CTX_data_main(C), filepath, fileflags, NULL, NULL);
539 BLI_strncpy(curundo->str, filepath, sizeof(curundo->str));
542 MemFile *prevfile=NULL;
544 if(curundo->prev) prevfile= &(curundo->prev->memfile);
546 memused= MEM_get_memory_in_use();
547 success= BLO_write_file_mem(CTX_data_main(C), prevfile, &curundo->memfile, G.fileflags);
548 curundo->undosize= MEM_get_memory_in_use() - memused;
551 if(U.undomemory != 0) {
552 /* limit to maximum memory (afterwards, we can't know in advance) */
554 maxmem= ((uintptr_t)U.undomemory)*1024*1024;
556 /* keep at least two (original + other) */
558 while(uel && uel->prev) {
559 totmem+= uel->undosize;
560 if(totmem>maxmem) break;
565 if(uel->prev && uel->prev->prev)
568 while(undobase.first!=uel) {
569 UndoElem *first= undobase.first;
570 BLI_remlink(&undobase, first);
571 /* the merge is because of compression */
572 BLO_merge_memfile(&first->memfile, &first->next->memfile);
579 /* 1= an undo, -1 is a redo. we have to make sure 'curundo' remains at current situation */
580 void BKE_undo_step(bContext *C, int step)
584 read_undosave(C, curundo);
587 /* curundo should never be NULL, after restart or load file it should call undo_save */
588 if(curundo==NULL || curundo->prev==NULL) ; // XXX error("No undo available");
590 if(G.f & G_DEBUG) printf("undo %s\n", curundo->name);
591 curundo= curundo->prev;
592 read_undosave(C, curundo);
597 /* curundo has to remain current situation! */
599 if(curundo==NULL || curundo->next==NULL) ; // XXX error("No redo available");
601 read_undosave(C, curundo->next);
602 curundo= curundo->next;
603 if(G.f & G_DEBUG) printf("redo %s\n", curundo->name);
608 void BKE_reset_undo(void)
614 BLO_free_memfile(&uel->memfile);
618 BLI_freelistN(&undobase);
622 /* based on index nr it does a restore */
623 void BKE_undo_number(bContext *C, int nr)
625 curundo= BLI_findlink(&undobase, nr);
629 /* go back to the last occurance of name in stack */
630 void BKE_undo_name(bContext *C, const char *name)
632 UndoElem *uel= BLI_rfindstring(&undobase, name, offsetof(UndoElem, name));
634 if(uel && uel->prev) {
641 int BKE_undo_valid(const char *name)
644 UndoElem *uel= BLI_rfindstring(&undobase, name, offsetof(UndoElem, name));
645 return uel && uel->prev;
648 return undobase.last != undobase.first;
651 /* get name of undo item, return null if no item with this index */
652 /* if active pointer, set it to 1 if true */
653 char *BKE_undo_get_name(int nr, int *active)
655 UndoElem *uel= BLI_findlink(&undobase, nr);
657 if(active) *active= 0;
660 if(active && uel==curundo)
667 char *BKE_undo_menu_string(void)
670 DynStr *ds= BLI_dynstr_new();
673 BLI_dynstr_append(ds, "Global Undo History %t");
675 for(uel= undobase.first; uel; uel= uel->next) {
676 BLI_dynstr_append(ds, "|");
677 BLI_dynstr_append(ds, uel->name);
680 menu= BLI_dynstr_get_cstring(ds);
686 /* saves quit.blend */
687 void BKE_undo_save_quit(void)
692 char str[FILE_MAXDIR+FILE_MAXFILE];
694 if( (U.uiflag & USER_GLOBALUNDO)==0) return;
698 printf("No undo buffer to save recovery file\n");
702 /* no undo state to save */
703 if(undobase.first==undobase.last) return;
705 BLI_make_file_string("/", str, btempdir, "quit.blend");
707 file = open(str,O_BINARY+O_WRONLY+O_CREAT+O_TRUNC, 0666);
709 //XXX error("Unable to save %s, check you have permissions", str);
713 chunk= uel->memfile.chunks.first;
715 if( write(file, chunk->buf, chunk->size) != chunk->size) break;
721 if(chunk) ; //XXX error("Unable to save %s, internal error", str);
722 else printf("Saved session recovery to %s\n", str);
726 Main *BKE_undo_get_main(Scene **scene)
729 BlendFileData *bfd= BLO_read_from_memfile(G.main, G.main->name, &curundo->memfile, NULL);
734 *scene= bfd->curscene;