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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 *****
34 #include <unistd.h> // for read close
35 #include <sys/param.h> // for MAXPATHLEN
37 #include <io.h> // for open close read
47 #include <fcntl.h> // for open
49 #include "MEM_guardedalloc.h"
51 #include "DNA_curve_types.h"
52 #include "DNA_listBase.h"
53 #include "DNA_sdna_types.h"
54 #include "DNA_userdef_types.h"
55 #include "DNA_object_types.h"
56 #include "DNA_mesh_types.h"
57 #include "DNA_scene_types.h"
58 #include "DNA_screen_types.h"
59 #include "DNA_sound_types.h"
60 #include "DNA_sequence_types.h"
62 #include "BLI_blenlib.h"
63 #include "BLI_dynstr.h"
65 #include "IMB_imbuf_types.h"
66 #include "IMB_imbuf.h"
68 #include "BKE_animsys.h"
69 #include "BKE_action.h"
70 #include "BKE_blender.h"
71 #include "BKE_context.h"
72 #include "BKE_curve.h"
73 #include "BKE_depsgraph.h"
74 #include "BKE_displist.h"
76 #include "BKE_global.h"
77 #include "BKE_idprop.h"
78 #include "BKE_library.h"
82 #include "BKE_object.h"
83 #include "BKE_report.h"
84 #include "BKE_scene.h"
85 #include "BKE_screen.h"
86 #include "BKE_sequencer.h"
87 #include "BKE_sound.h"
89 #include "BLI_editVert.h"
91 #include "BLO_undofile.h"
92 #include "BLO_readfile.h"
93 #include "BLO_writefile.h"
95 #include "BKE_utildefines.h" // O_BINARY FALSE
97 #include "WM_api.h" // XXXXX BAD, very BAD dependency (bad level call) - remove asap, elubie
101 ListBase WMlist= {NULL, NULL};
104 char versionstr[48]= "";
106 /* ************************************************ */
107 /* pushpop facility: to store data temporally, FIFO! */
109 ListBase ppmain={0, 0};
111 typedef struct PushPop {
112 struct PushPop *next, *prev;
117 void pushdata(void *data, int len)
121 pp= MEM_mallocN(sizeof(PushPop), "pushpop");
122 BLI_addtail(&ppmain, pp);
123 pp->data= MEM_mallocN(len, "pushpop");
125 memcpy(pp->data, data, len);
128 void popfirst(void *data)
134 memcpy(data, pp->data, pp->len);
135 BLI_remlink(&ppmain, pp);
139 else printf("error in popfirst\n");
142 void poplast(void *data)
148 memcpy(data, pp->data, pp->len);
149 BLI_remlink(&ppmain, pp);
153 else printf("error in poplast\n");
162 BLI_remlink(&ppmain, pp);
170 if(ppmain.first) printf("pushpop not empty\n");
176 /* ********** free ********** */
178 /* only to be called on exit blender */
179 void free_blender(void)
181 /* samples are in a global list..., also sets G.main->sound->sample NULL */
185 BKE_spacetypes_free(); /* after free main, it uses space callbacks */
187 IMB_freeImBufdata(); /* imbuf lib */
192 void initglobals(void)
194 memset(&G, 0, sizeof(Global));
198 G.main= MEM_callocN(sizeof(Main), "initglobals");
203 ENDIAN_ORDER= (((char*)&ENDIAN_ORDER)[0])? L_ENDIAN: B_ENDIAN;
205 if(BLENDER_SUBVERSION)
206 sprintf(versionstr, "www.blender.org %d.%d", BLENDER_VERSION, BLENDER_SUBVERSION);
208 sprintf(versionstr, "www.blender.org %d", BLENDER_VERSION);
210 #ifdef _WIN32 // FULLSCREEN
211 G.windowstate = G_WINDOWSTATE_USERDEF;
214 G.charstart = 0x0000;
221 static void clear_global(void)
223 // extern short winqueue_break; /* screen.c */
225 fastshade_free_render(); /* lamps hang otherwise */
226 free_main(G.main); /* free all lib data */
228 // free_vertexpaint();
233 /* make sure path names are correct for OS */
234 static void clean_paths(Main *main)
236 Image *image= main->image.first;
237 bSound *sound= main->sound.first;
238 Scene *scene= main->scene.first;
244 BLI_clean(image->name);
245 image= image->id.next;
249 BLI_clean(sound->name);
250 sound= sound->id.next;
254 ed= seq_give_editing(scene, 0);
256 seq= ed->seqbasep->first;
259 BLI_clean(seq->plugin->name);
263 BLI_clean(strip->dir);
269 BLI_clean(scene->r.backbuf);
270 BLI_clean(scene->r.pic);
272 scene= scene->id.next;
276 /* context matching */
277 /* handle no-ui case */
279 static void setup_app_data(bContext *C, BlendFileData *bfd, char *filename)
282 bScreen *curscreen= NULL;
283 Scene *curscene= NULL;
287 /* 'u' = undo save, 'n' = no UI load */
288 if(bfd->main->screen.first==NULL) mode= 'u';
289 else if(G.fileflags & G_FILE_NO_UI) mode= 'n';
292 recover= (G.fileflags & G_FILE_RECOVER);
294 clean_paths(bfd->main);
296 /* XXX here the complex windowmanager matching */
298 /* no load screens? */
300 /* comes from readfile.c */
301 extern void lib_link_screen_restore(Main *, bScreen *, Scene *);
303 SWAP(ListBase, G.main->wm, bfd->main->wm);
304 SWAP(ListBase, G.main->screen, bfd->main->screen);
305 SWAP(ListBase, G.main->script, bfd->main->script);
307 /* we re-use current screen */
308 curscreen= CTX_wm_screen(C);
309 /* but use new Scene pointer */
310 curscene= bfd->curscene;
311 if(curscene==NULL) curscene= bfd->main->scene.first;
312 /* and we enforce curscene to be in current screen */
313 curscreen->scene= curscene;
315 /* clear_global will free G.main, here we can still restore pointers */
316 lib_link_screen_restore(bfd->main, curscreen, curscene);
319 /* free G.main Main database */
324 CTX_data_main_set(C, G.main);
328 /* only here free userdef themes... */
332 MEM_freeN(bfd->user);
335 /* case G_FILE_NO_UI or no screens in file */
337 /* leave entire context further unaltered? */
338 CTX_data_scene_set(C, curscene);
341 G.winpos= bfd->winpos;
342 G.displaymode= bfd->displaymode;
343 G.fileflags= bfd->fileflags;
345 CTX_wm_screen_set(C, bfd->curscreen);
346 CTX_data_scene_set(C, bfd->curscreen->scene);
347 CTX_wm_area_set(C, NULL);
348 CTX_wm_region_set(C, NULL);
349 CTX_wm_menu_set(C, NULL);
352 /* this can happen when active scene was lib-linked, and doesnt exist anymore */
353 if(CTX_data_scene(C)==NULL) {
354 CTX_data_scene_set(C, bfd->main->scene.first);
355 CTX_wm_screen(C)->scene= CTX_data_scene(C);
356 curscene= CTX_data_scene(C);
359 /* special cases, override loaded flags: */
360 if (G.f & G_DEBUG) bfd->globalf |= G_DEBUG;
361 else bfd->globalf &= ~G_DEBUG;
362 if (G.f & G_SWAP_EXCHANGE) bfd->globalf |= G_SWAP_EXCHANGE;
363 else bfd->globalf &= ~G_SWAP_EXCHANGE;
365 if ((U.flag & USER_DONT_DOSCRIPTLINKS)) bfd->globalf &= ~G_DOSCRIPTLINKS;
370 //setscreen(G.curscreen);
373 // XXX temporarily here
374 if(G.main->versionfile < 250)
375 do_versions_ipos_to_animato(G.main); // XXX fixme... complicated versionpatching
377 /* in case of autosave or quit.blend, use original filename instead */
378 if(recover && bfd->filename[0])
379 filename= bfd->filename;
381 /* these are the same at times, should never copy to the same location */
382 if(G.sce != filename)
383 BLI_strncpy(G.sce, filename, FILE_MAX);
385 BLI_strncpy(G.main->name, filename, FILE_MAX); /* is guaranteed current file */
387 /* baseflags, groups, make depsgraph, etc */
388 set_scene_bg(CTX_data_scene(C));
390 /* last stage of do_versions actually, that sets recalc flags for recalc poses */
391 for(ob= G.main->object.first; ob; ob= ob->id.next) {
392 if(ob->type==OB_ARMATURE)
393 if(ob->recalc) object_handle_update(CTX_data_scene(C), ob);
396 /* now tag update flags, to ensure deformers get calculated on redraw */
397 DAG_scene_update_flags(CTX_data_scene(C), CTX_data_scene(C)->lay);
402 static int handle_subversion_warning(Main *main)
404 if(main->minversionfile > BLENDER_VERSION ||
405 (main->minversionfile == BLENDER_VERSION &&
406 main->minsubversionfile > BLENDER_SUBVERSION)) {
410 sprintf(str, "File written by newer Blender binary: %d.%d , expect loss of data!", main->minversionfile, main->minsubversionfile);
416 void BKE_userdef_free(void)
421 for(km=U.keymaps.first; km; km=km->next) {
422 for(kmi=km->items.first; kmi; kmi=kmi->next) {
423 if(kmi->properties) {
424 IDP_FreeProperty(kmi->properties);
425 MEM_freeN(kmi->properties);
431 BLI_freelistN(&km->items);
434 BLI_freelistN(&U.uistyles);
435 BLI_freelistN(&U.uifonts);
436 BLI_freelistN(&U.themes);
437 BLI_freelistN(&U.keymaps);
444 2: OK, and with new user settings
447 int BKE_read_file(bContext *C, char *dir, void *unused, ReportList *reports)
452 bfd= BLO_read_from_file(dir, reports);
454 if(bfd->user) retval= 2;
456 if(0==handle_subversion_warning(bfd->main)) {
457 free_main(bfd->main);
463 setup_app_data(C, bfd, dir); // frees BFD
466 BKE_reports_prependf(reports, "Loading %s failed: ", dir);
468 return (bfd?retval:0);
471 int BKE_read_file_from_memory(bContext *C, char* filebuf, int filelength, void *unused, ReportList *reports)
475 bfd= BLO_read_from_memory(filebuf, filelength, reports);
477 setup_app_data(C, bfd, "<memory2>");
479 BKE_reports_prepend(reports, "Loading failed: ");
484 /* memfile is the undo buffer */
485 int BKE_read_file_from_memfile(bContext *C, MemFile *memfile, ReportList *reports)
489 bfd= BLO_read_from_memfile(CTX_data_main(C), G.sce, memfile, reports);
491 setup_app_data(C, bfd, "<memory1>");
493 BKE_reports_prepend(reports, "Loading failed: ");
498 /* ***************** testing for break ************* */
500 static void (*blender_test_break_cb)(void)= NULL;
502 void set_blender_test_break_cb(void (*func)(void) )
504 blender_test_break_cb= func;
508 int blender_test_break(void)
511 if (blender_test_break_cb)
512 blender_test_break_cb();
515 return (G.afbreek==1);
519 /* ***************** GLOBAL UNDO *************** */
523 #define MAXUNDONAME 64
524 typedef struct UndoElem {
525 struct UndoElem *next, *prev;
526 char str[FILE_MAXDIR+FILE_MAXFILE];
527 char name[MAXUNDONAME];
532 static ListBase undobase={NULL, NULL};
533 static UndoElem *curundo= NULL;
536 static int read_undosave(bContext *C, UndoElem *uel)
538 char scestr[FILE_MAXDIR+FILE_MAXFILE];
539 int success=0, fileflags;
541 /* This is needed so undoing/redoing doesnt crash with threaded previews going */
542 WM_jobs_stop_all(CTX_wm_manager(C));
544 strcpy(scestr, G.sce); /* temporal store */
545 fileflags= G.fileflags;
546 G.fileflags |= G_FILE_NO_UI;
549 success= BKE_read_file(C, uel->str, NULL, NULL);
551 success= BKE_read_file_from_memfile(C, &uel->memfile, NULL);
554 strcpy(G.sce, scestr);
555 G.fileflags= fileflags;
560 /* name can be a dynamic string */
561 void BKE_write_undo(bContext *C, char *name)
563 uintptr_t maxmem, totmem, memused;
567 if( (U.uiflag & USER_GLOBALUNDO)==0) return;
568 if( U.undosteps==0) return;
570 /* remove all undos after (also when curundo==NULL) */
571 while(undobase.last != curundo) {
573 BLI_remlink(&undobase, uel);
574 BLO_free_memfile(&uel->memfile);
579 curundo= uel= MEM_callocN(sizeof(UndoElem), "undo file");
580 strncpy(uel->name, name, MAXUNDONAME-1);
581 BLI_addtail(&undobase, uel);
583 /* and limit amount to the maximum */
588 if(nr==U.undosteps) break;
592 while(undobase.first!=uel) {
593 UndoElem *first= undobase.first;
594 BLI_remlink(&undobase, first);
595 /* the merge is because of compression */
596 BLO_merge_memfile(&first->memfile, &first->next->memfile);
602 /* disk save version */
604 static int counter= 0;
605 char tstr[FILE_MAXDIR+FILE_MAXFILE];
608 /* calculate current filename */
610 counter= counter % U.undosteps;
612 sprintf(numstr, "%d.blend", counter);
613 BLI_make_file_string("/", tstr, btempdir, numstr);
615 success= BLO_write_file(CTX_data_main(C), tstr, G.fileflags, NULL);
617 strcpy(curundo->str, tstr);
620 MemFile *prevfile=NULL;
622 if(curundo->prev) prevfile= &(curundo->prev->memfile);
624 memused= MEM_get_memory_in_use();
625 success= BLO_write_file_mem(CTX_data_main(C), prevfile, &curundo->memfile, G.fileflags, NULL);
626 curundo->undosize= MEM_get_memory_in_use() - memused;
629 if(U.undomemory != 0) {
630 /* limit to maximum memory (afterwards, we can't know in advance) */
632 maxmem= ((uintptr_t)U.undomemory)*1024*1024;
634 /* keep at least two (original + other) */
636 while(uel && uel->prev) {
637 totmem+= uel->undosize;
638 if(totmem>maxmem) break;
643 if(uel->prev && uel->prev->prev)
646 while(undobase.first!=uel) {
647 UndoElem *first= undobase.first;
648 BLI_remlink(&undobase, first);
649 /* the merge is because of compression */
650 BLO_merge_memfile(&first->memfile, &first->next->memfile);
657 /* 1= an undo, -1 is a redo. we have to make sure 'curundo' remains at current situation */
658 void BKE_undo_step(bContext *C, int step)
662 read_undosave(C, curundo);
665 /* curundo should never be NULL, after restart or load file it should call undo_save */
666 if(curundo==NULL || curundo->prev==NULL) ; // XXX error("No undo available");
668 if(G.f & G_DEBUG) printf("undo %s\n", curundo->name);
669 curundo= curundo->prev;
670 read_undosave(C, curundo);
675 /* curundo has to remain current situation! */
677 if(curundo==NULL || curundo->next==NULL) ; // XXX error("No redo available");
679 read_undosave(C, curundo->next);
680 curundo= curundo->next;
681 if(G.f & G_DEBUG) printf("redo %s\n", curundo->name);
686 void BKE_reset_undo(void)
692 BLO_free_memfile(&uel->memfile);
696 BLI_freelistN(&undobase);
700 /* based on index nr it does a restore */
701 void BKE_undo_number(bContext *C, int nr)
706 for(uel= undobase.first; uel; uel= uel->next, a++) {
713 /* go back to the last occurance of name in stack */
714 void BKE_undo_name(bContext *C, const char *name)
718 for(uel= undobase.last; uel; uel= uel->prev) {
719 if(strcmp(name, uel->name)==0)
722 if(uel && uel->prev) {
729 char *BKE_undo_menu_string(void)
732 DynStr *ds= BLI_dynstr_new();
735 BLI_dynstr_append(ds, "Global Undo History %t");
737 for(uel= undobase.first; uel; uel= uel->next) {
738 BLI_dynstr_append(ds, "|");
739 BLI_dynstr_append(ds, uel->name);
742 menu= BLI_dynstr_get_cstring(ds);
748 /* saves quit.blend */
749 void BKE_undo_save_quit(void)
754 char str[FILE_MAXDIR+FILE_MAXFILE];
756 if( (U.uiflag & USER_GLOBALUNDO)==0) return;
760 printf("No undo buffer to save recovery file\n");
764 /* no undo state to save */
765 if(undobase.first==undobase.last) return;
767 BLI_make_file_string("/", str, btempdir, "quit.blend");
769 file = open(str,O_BINARY+O_WRONLY+O_CREAT+O_TRUNC, 0666);
771 //XXX error("Unable to save %s, check you have permissions", str);
775 chunk= uel->memfile.chunks.first;
777 if( write(file, chunk->buf, chunk->size) != chunk->size) break;
783 if(chunk) ; //XXX error("Unable to save %s, internal error", str);
784 else printf("Saved session recovery to %s\n", str);