This repository has been archived on 2023-10-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-archive/source/blender/blenkernel/intern/blender.c

627 lines
13 KiB
C
Raw Normal View History

2002-10-12 11:37:38 +00:00
/* blender.c jan 94 MIXED MODEL
*
* common help functions and data
2002-10-12 11:37:38 +00:00
*
* $Id$
*
* ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version. The Blender
* Foundation also sells licenses for use in proprietary software under
* the Blender License. See http://www.blender.org/BL/ for information
* about this.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): none yet.
*
* ***** END GPL/BL DUAL LICENSE BLOCK *****
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
2002-10-12 11:37:38 +00:00
#ifndef WIN32
#include <unistd.h> // for read close
#include <sys/param.h> // for MAXPATHLEN
#else
#include <io.h> // for open close read
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h> // for open
#include "MEM_guardedalloc.h"
#include "DNA_listBase.h"
#include "DNA_sdna_types.h"
#include "DNA_userdef_types.h"
#include "DNA_object_types.h"
#include "DNA_curve_types.h"
#include "BLI_blenlib.h"
#include "IMB_imbuf_types.h"
#include "IMB_imbuf.h"
#ifdef WIN32
#include "BLI_winstuff.h"
#endif
#include "DNA_mesh_types.h"
2002-10-12 11:37:38 +00:00
#include "DNA_screen_types.h"
#include "BKE_library.h"
#include "BKE_blender.h"
#include "BKE_displist.h"
#include "BKE_global.h"
#include "BKE_main.h"
#include "BKE_object.h"
#include "BKE_scene.h"
#include "BKE_curve.h"
#include "BKE_font.h"
#include "BLI_editVert.h"
2002-10-12 11:37:38 +00:00
#include "BLO_undofile.h"
Another step in the undo evolution. - Made unified API for undo calls, to be found in space.c BIF_undo_push(char *str) BIF_undo(void) BIF_redo(void) These calls will do all undo levels, including editmode and vpaint. The transition is work in progress, because mesh undo needs recode. - New global hotkey CTR+Z for undo Note: 'shaded draw mode' still is SHIFT+Z, the old CTRL+Z was to recalc the lighting in shaded mode, which already became much more interactive, like during/after any transform(). Recalc hotkey now is SHIFT+ALT+Z CTRL+<any modifier>+Z is redo. - For OSX users; the Apple-key ("Command") now maps to CTRL as well. This disables the one-mouse-button hack for rightmouse btw, will be fixed in next commit. At least we can use Apple-Z :) - Old Ukey for undo is still there, as a training period... my preference is to restore Ukey to "reload original data" as in past, and only use new CTRL+Z for undo. - Added undo_push() for all of editobject.c and editview.c. Meaning we can start using/testing global undo in the 3d window. Please dont comment on missing parts for now, first I want someone to volunteer to tackle all of that. - Since the global undo has a full 'file' in memory, it can save extremely fast on exit to <temp dir>/quit.blend. That's default now when global undo is enabled. It prints "Saved session recovery to ..." in console then. - In file menu, a new option is added "Recover Last Session". Note that this reads the undo-save, which is without UI. - With such nice new features we then can also kill the disputed Cancel/Confirm menu on Q-KEY. - Added fix which initializes seam/normal theme color on saved themes. They showed black now.... (Note: that's in usiblender.c!)
2004-09-18 12:12:45 +00:00
#include "BLO_readfile.h"
#include "BLO_writefile.h"
2002-10-12 11:37:38 +00:00
#include "BKE_bad_level_calls.h" // for freeAllRad editNurb free_editMesh free_editText free_editArmature
#include "BKE_utildefines.h" // O_BINARY FALSE
#include "BIF_mainqueue.h" // mainqenter for onload script
#include "mydevice.h"
2002-10-12 11:37:38 +00:00
#include "nla.h"
Global G;
UserDef U;
char versionstr[48]= "";
/* ************************************************ */
/* pushpop facility: to store data temporally, FIFO! */
2002-10-12 11:37:38 +00:00
ListBase ppmain={0, 0};
typedef struct PushPop {
struct PushPop *next, *prev;
void *data;
int len;
} PushPop;
void pushdata(void *data, int len)
{
PushPop *pp;
pp= MEM_mallocN(sizeof(PushPop), "pushpop");
BLI_addtail(&ppmain, pp);
pp->data= MEM_mallocN(len, "pushpop");
pp->len= len;
memcpy(pp->data, data, len);
}
void popfirst(void *data)
{
PushPop *pp;
pp= ppmain.first;
if(pp) {
memcpy(data, pp->data, pp->len);
BLI_remlink(&ppmain, pp);
MEM_freeN(pp->data);
MEM_freeN(pp);
}
else printf("error in popfirst\n");
}
void poplast(void *data)
{
PushPop *pp;
pp= ppmain.last;
if(pp) {
memcpy(data, pp->data, pp->len);
BLI_remlink(&ppmain, pp);
MEM_freeN(pp->data);
MEM_freeN(pp);
}
else printf("error in poplast\n");
}
void free_pushpop()
{
PushPop *pp;
pp= ppmain.first;
while(pp) {
BLI_remlink(&ppmain, pp);
MEM_freeN(pp->data);
MEM_freeN(pp);
}
}
void pushpop_test()
{
if(ppmain.first) printf("pushpop not empty\n");
free_pushpop();
}
/* ********** free ********** */
2002-10-12 11:37:38 +00:00
void free_blender(void)
{
free_main(G.main);
G.main= NULL;
IMB_freeImBufdata(); /* imbuf lib */
}
void duplicatelist(ListBase *list1, ListBase *list2) /* copy from 2 to 1 */
2002-10-12 11:37:38 +00:00
{
struct Link *link1, *link2;
list1->first= list1->last= 0;
link2= list2->first;
while(link2) {
link1= MEM_dupallocN(link2);
BLI_addtail(list1, link1);
link2= link2->next;
}
}
static EditMesh theEditMesh;
2002-10-12 11:37:38 +00:00
void initglobals(void)
{
memset(&G, 0, sizeof(Global));
G.editMesh = &theEditMesh;
memset(G.editMesh, 0, sizeof(G.editMesh));
2002-10-12 11:37:38 +00:00
U.savetime= 1;
G.animspeed= 4;
G.main= MEM_callocN(sizeof(Main), "initglobals");
strcpy(G.ima, "//");
G.version= BLENDER_VERSION;
G.order= 1;
G.order= (((char*)&G.order)[0])?L_ENDIAN:B_ENDIAN;
sprintf(versionstr, "www.blender.org %d", G.version);
2002-10-12 11:37:38 +00:00
#ifdef _WIN32 // FULLSCREEN
G.windowstate = G_WINDOWSTATE_USERDEF;
#endif
2002-10-12 11:37:38 +00:00
clear_workob(); /* object.c */
}
/***/
static void clear_global(void)
{
2002-10-12 11:37:38 +00:00
extern short winqueue_break; /* screen.c */
freeAllRad();
free_main(G.main); /* free all lib data */
freefastshade(); /* othwerwise old lamp settings stay active */
2002-10-12 11:37:38 +00:00
/* prevent hanging vars */
2002-10-12 11:37:38 +00:00
R.backbuf= 0;
/* force all queues to be left */
winqueue_break= 1;
if (G.obedit) {
freeNurblist(&editNurb);
EditMesh refactory + undo recode The changelog is very long... it's on the web too: http://www.blender3d.org/cms/Mesh_editing_rewrite.425.0.html EditMesh refactor notes (user) **** New selection modes When entering Edit Mode for a Mesh, you now have the choice for three selection modes. These are shown as icons in the 3D header (hotkey is being searched for!). - Vertex Select Select vertices as usual, fully compatible with how previous version work - Edge Select Vertices are not drawn anymore, and selections happen by default on the edges. It is a true edge select, meaning that you can select three out of four edges in a face, without automatic having the 4th edge selected. - Face Select Instead of vertices, now selection 'points' are drawn in the face centers. Selected faces also get a colored outline, like for edges. This also is true face select, for each face individual regardless selection status of its vertices or edges. While holding SHIFT, and press a selection mode, you can also combine the above choices. Now selection becomes mixed, and will behave as expected. For example; in Edge+Face select mode, selecting the 4 edges of a face will select the face too. The selection modes and optional drawing modes (like transparant faces, normals, or solid drawing) all work together. All of Blender's mesh editing tools now react to the correct selection mode as well. Most noticeable it's in: **** Extrude Extruding in Edge or Face Select mode allows much more precise control over what's extruded and what should be excluded. Try for example a checker pattern selection, and extrude it. New is the fixed translation when faces are extruded. This always follows the (averaged) face normal(s) of the old face(s), enabling much easier working in 3D views . A single 'G' (Grab) or 'R' (Rotate) or 'S' (Scale) will change transform modus as usual. **** Other things to note - Hiding edges/faces will also behave different based on Select Mode. - while editing, normals of faces are updated always now - Border select (BKEY) has 2 different rules for edges; when one edge is fully inside of the border, it will only select edges that are fully inside. Otherwise it selects each edge intersecting with the border. - in face mode, adding vertices, edges or a circle is invisible... - "Add monkey" now works as a normal primitive (rotated and on 3d cursor) - Mesh undo was fully recoded, hopefully solving issues now with Vertex Keys and Groups - Going in and out of editmode was fully recoded. Especially on larger models you'll notice substantial speed gain. **** Todo Add 'FaceSelect mode' functionality in EditMode, including zbuffered selection, display and editing of UV texture. EditMesh refactor notes (coder) **** Usage of flags in general The "->f" flags are reserved for the editmesh.c and editmesh_lib.c core functions. Actually only selection status is there now. The "->f1" and "->f2" flags are free to use. They're available in vertex/edge/face structs. Since they're free, check carefully when calling other functions that use these flags... for example extrude() or subdivide() use them. **** Selection flags EditVert: eve->f & SELECT EditEdge: eed->f & SELECT EditFace: efa->f & SELECT - Selection is only possible when not-hidden! - Selection flags are always up-to-date, BUT: if selection mode >= SELECT_EDGE vertex selection flags can be incorrect if selection mode == SELECT_FACE vertex/edge selection flags can be incorrect This because of shared vertices or edges. - use for selecting vertices: eve->f &= SELECT - use for selecting edges always: void EM_select_edge(eed, 1) // 1 = select, 0 = deselect - use for selecting faces always: void EM_select_face(efa, 1) // 1 = select, 0 = deselect - To set the 'f' flags in all of the data: void EM_set_flag_all(int flag); void EM_clear_flag_all(int flag); - the old faceselectedOR() and faceselectedAND() are still there, but only to be used for evaluating its vertices **** Code hints for handling selection If the selectmode is 'face'; vertex or edge selections need to be flushed upward. Same is true for 'edge' selection mode. This means that you'll have to keep track of all selections while coding... selecting the four vertices in a face doesn't automatically select the face anymore. However, by using the above calls, at least selections flush downward (to vertex level). You then can call: void EM_selectmode_flush(void); Which flushes selections back upward, based on the selectmode setting. This function does the following: - if selectmode 'vertex': select edges/faces based on its selected vertices - if selectmode 'edge': select faces based its selected edges This works fine in nice controlled situations. However, only changing the vertex selections then still doesn't select a face in face mode! If you really can't avoid only working with vertex selections, you can use this call: void EM_select_flush(void); Now selection is flushed upward regardless current selectmode. That can be destructive for special cases however, like checkerboard selected faces. So use this only when you know everything else was deselected (or deselect it). Example: adding primitives. **** Hide flags EditVert: eve->h EditEdge: eed->h EditFace: efa->h - all hide flags are always up-to-date - hidden vertices/edges/faces are always deselected. so when you operate on selection only, there's no need to check for hide flag. **** Unified undo for editmode New file: editmode_undo.h A pretty nice function pointer handler style undo. Just code three functions, and your undo will fly! The c file has a good reference. Also note that the old undo system has been replaced. It currently uses minimal dependencies on Meshes themselves (no abuse of going in/out editmode), and is restricted nicely to editmode functions. **** Going in/out editmode As speedup now all vertices/faces/edges are allocated in three big chunks. In vertices/faces/edges now tags are set to denote such data cannot be freed. ALso the hashtable (lookup) for edges uses no mallocs at all anymore, but is part of the EditEdge itself.
2004-09-23 20:52:51 +00:00
free_editMesh(G.editMesh);
2002-10-12 11:37:38 +00:00
free_editText();
free_editArmature();
}
G.curscreen= NULL;
G.scene= NULL;
G.main= NULL;
G.obedit= NULL;
G.obpose= NULL;
G.saction= NULL;
G.buts= NULL;
G.v2d= NULL;
G.vd= NULL;
G.soops= NULL;
G.sima= NULL;
G.sipo= NULL;
G.f &= ~(G_WEIGHTPAINT + G_VERTEXPAINT + G_FACESELECT);
}
static void setup_app_data(BlendFileData *bfd, char *filename)
{
2002-10-12 11:37:38 +00:00
Object *ob;
bScreen *curscreen= NULL;
Scene *curscene= NULL;
char mode;
/* 'u' = undo save, 'n' = no UI load */
if(bfd->main->screen.first==NULL) mode= 'u';
else if(G.fileflags & G_FILE_NO_UI) mode= 'n';
else mode= 0;
/* no load screens? */
if(mode) {
/* comes from readfile.c */
extern void lib_link_screen_restore(Main *, char, Scene *);
SWAP(ListBase, G.main->screen, bfd->main->screen);
/* we re-use current screen */
curscreen= G.curscreen;
/* but use new Scene pointer */
curscene= bfd->curscene;
if(curscene==NULL) curscene= bfd->main->scene.first;
/* and we enforce curscene to be in current screen */
curscreen->scene= curscene;
/* clear_global will free G.main, here we can still restore pointers */
lib_link_screen_restore(bfd->main, mode, curscene);
}
2002-10-12 11:37:38 +00:00
clear_global();
G.save_over = 1;
G.main= bfd->main;
if (bfd->user) {
U= *bfd->user;
MEM_freeN(bfd->user);
/* the UserDef struct is not corrected with do_versions() .... ugh! */
if(U.wheellinescroll == 0) U.wheellinescroll = 3;
if(U.menuthreshold1==0) {
U.menuthreshold1= 5;
U.menuthreshold2= 2;
}
if(U.tb_leftmouse==0) {
U.tb_leftmouse= 5;
U.tb_rightmouse= 5;
}
if(U.mixbufsize==0) U.mixbufsize= 2048;
2002-10-12 11:37:38 +00:00
}
/* case G_FILE_NO_UI or no screens in file */
if(mode) {
G.curscreen= curscreen;
G.scene= curscene;
}
else {
R.winpos= bfd->winpos;
R.displaymode= bfd->displaymode;
G.fileflags= bfd->fileflags;
G.curscreen= bfd->curscreen;
G.scene= G.curscreen->scene;
}
/* special cases, override loaded flags: */
New scripts: - hotkeys, obdatacopier and renameobjectbyblock, all from Jean-Michel Soler (jms); - bevel_center by Loic Berthe, suggested for inclusion by jms; - doc_browser, by Daniel Dunbar (Zr) Thanks to them for the new contributions! (I included doc_browser at 'Misc' because only users interested in script writing would actually use it, but it could also be under 'Help'. Opinions?) BPython related: - Added scriptlink methods to object, lamp, camera and world. - Object: added object.makeTrack and object.clearTrack (old track method). - sys: made sys.exists(path) return 0 for not found; 1 for file, 2 for dir and -1 for neither. - doc updates and fixes. - made ONLOAD event work. G.f's SCENESCRIPT bit was being zeroed in set_app_data. - Blender: updated functions Load and Save to support the builtin importers and exporters besides .blend (dxf, videoscape, vrml 1.0, stl, ...) - Draw: added mouse wheel events. - Scene: added scene.play to play back animations (like ALT+A and SHIFT+ALT+A). Makes a good counter, too, when the 'win' attribute is set to a space that doesn't "animate". The scene.play() addition and the fix to ONLOAD scriptlinks is part of the work for a Blender demo mode. It already works, but I'll still add support for Radiosity calculations and fix a thing in main(): it executes onload scripts too early (BIF_Init), giving funny results in alt+a animations and renderings when firing up Blender. Loading after the program is up has no such problems. When I finish I'll post examples of demo mode scripts.
2004-07-03 05:17:04 +00:00
if (G.f & G_DEBUG) bfd->globalf |= G_DEBUG;
else bfd->globalf &= ~G_DEBUG;
New scripts: - hotkeys, obdatacopier and renameobjectbyblock, all from Jean-Michel Soler (jms); - bevel_center by Loic Berthe, suggested for inclusion by jms; - doc_browser, by Daniel Dunbar (Zr) Thanks to them for the new contributions! (I included doc_browser at 'Misc' because only users interested in script writing would actually use it, but it could also be under 'Help'. Opinions?) BPython related: - Added scriptlink methods to object, lamp, camera and world. - Object: added object.makeTrack and object.clearTrack (old track method). - sys: made sys.exists(path) return 0 for not found; 1 for file, 2 for dir and -1 for neither. - doc updates and fixes. - made ONLOAD event work. G.f's SCENESCRIPT bit was being zeroed in set_app_data. - Blender: updated functions Load and Save to support the builtin importers and exporters besides .blend (dxf, videoscape, vrml 1.0, stl, ...) - Draw: added mouse wheel events. - Scene: added scene.play to play back animations (like ALT+A and SHIFT+ALT+A). Makes a good counter, too, when the 'win' attribute is set to a space that doesn't "animate". The scene.play() addition and the fix to ONLOAD scriptlinks is part of the work for a Blender demo mode. It already works, but I'll still add support for Radiosity calculations and fix a thing in main(): it executes onload scripts too early (BIF_Init), giving funny results in alt+a animations and renderings when firing up Blender. Loading after the program is up has no such problems. When I finish I'll post examples of demo mode scripts.
2004-07-03 05:17:04 +00:00
if (G.f & G_SCENESCRIPT) bfd->globalf |= G_SCENESCRIPT;
else bfd->globalf &= ~G_SCENESCRIPT;
G.f= bfd->globalf;
2002-10-12 11:37:38 +00:00
/* few DispLists, but do text_to_curve */
2002-10-12 11:37:38 +00:00
// this should be removed!!! But first a better displist system (ton)
for (ob= G.main->object.first; ob; ob= ob->id.next) {
if(ob->type==OB_FONT) {
Curve *cu= ob->data;
if(cu->nurb.first==0) text_to_curve(ob, 0);
}
}
if (!G.background) {
setscreen(G.curscreen);
}
/* baseflags */
set_scene_bg(G.scene);
2002-10-12 11:37:38 +00:00
if (G.f & G_SCENESCRIPT) {
/* there's an onload scriptlink to execute in screenmain */
mainqenter(ONLOAD_SCRIPT, 1);
2002-10-12 11:37:38 +00:00
}
2002-10-12 11:37:38 +00:00
strcpy(G.sce, filename);
strcpy(G.main->name, filename); /* is guaranteed current file */
2002-10-12 11:37:38 +00:00
MEM_freeN(bfd);
}
int BKE_read_file(char *dir, void *type_r)
{
2002-10-12 11:37:38 +00:00
BlendReadError bre;
BlendFileData *bfd;
if (!G.background)
waitcursor(1);
bfd= BLO_read_from_file(dir, &bre);
if (bfd) {
if (type_r)
*((BlenFileType*)type_r)= bfd->type;
setup_app_data(bfd, dir);
} else {
error("Loading %s failed: %s", dir, BLO_bre_as_string(bre));
}
if (!G.background)
waitcursor(0);
return (bfd?1:0);
}
int BKE_read_file_from_memory(char* filebuf, int filelength, void *type_r)
{
BlendReadError bre;
BlendFileData *bfd;
if (!G.background)
waitcursor(1);
bfd= BLO_read_from_memory(filebuf, filelength, &bre);
if (bfd) {
if (type_r)
*((BlenFileType*)type_r)= bfd->type;
setup_app_data(bfd, "<memory>");
} else {
error("Loading failed: %s", BLO_bre_as_string(bre));
}
if (!G.background)
waitcursor(0);
return (bfd?1:0);
}
int BKE_read_file_from_memfile(MemFile *memfile)
{
BlendReadError bre;
BlendFileData *bfd;
if (!G.background)
waitcursor(1);
bfd= BLO_read_from_memfile(memfile, &bre);
if (bfd) {
setup_app_data(bfd, "<memory>");
} else {
error("Loading failed: %s", BLO_bre_as_string(bre));
}
if (!G.background)
waitcursor(0);
return (bfd?1:0);
}
Another step in the undo evolution. - Made unified API for undo calls, to be found in space.c BIF_undo_push(char *str) BIF_undo(void) BIF_redo(void) These calls will do all undo levels, including editmode and vpaint. The transition is work in progress, because mesh undo needs recode. - New global hotkey CTR+Z for undo Note: 'shaded draw mode' still is SHIFT+Z, the old CTRL+Z was to recalc the lighting in shaded mode, which already became much more interactive, like during/after any transform(). Recalc hotkey now is SHIFT+ALT+Z CTRL+<any modifier>+Z is redo. - For OSX users; the Apple-key ("Command") now maps to CTRL as well. This disables the one-mouse-button hack for rightmouse btw, will be fixed in next commit. At least we can use Apple-Z :) - Old Ukey for undo is still there, as a training period... my preference is to restore Ukey to "reload original data" as in past, and only use new CTRL+Z for undo. - Added undo_push() for all of editobject.c and editview.c. Meaning we can start using/testing global undo in the 3d window. Please dont comment on missing parts for now, first I want someone to volunteer to tackle all of that. - Since the global undo has a full 'file' in memory, it can save extremely fast on exit to <temp dir>/quit.blend. That's default now when global undo is enabled. It prints "Saved session recovery to ..." in console then. - In file menu, a new option is added "Recover Last Session". Note that this reads the undo-save, which is without UI. - With such nice new features we then can also kill the disputed Cancel/Confirm menu on Q-KEY. - Added fix which initializes seam/normal theme color on saved themes. They showed black now.... (Note: that's in usiblender.c!)
2004-09-18 12:12:45 +00:00
/* ***************** GLOBAL UNDO *************** */
#define UNDO_DISK 0
#define MAXUNDONAME 64
typedef struct UndoElem {
struct UndoElem *next, *prev;
char str[FILE_MAXDIR+FILE_MAXFILE];
char name[MAXUNDONAME];
MemFile memfile;
} UndoElem;
#define MAXUNDO 32
static ListBase undobase={NULL, NULL};
static UndoElem *curundo= NULL;
static int read_undosave(UndoElem *uel)
{
char scestr[FILE_MAXDIR+FILE_MAXFILE];
int success=0, fileflags;
strcpy(scestr, G.sce); /* temporal store */
fileflags= G.fileflags;
G.fileflags |= G_FILE_NO_UI;
if(UNDO_DISK)
success= BKE_read_file(uel->str, NULL);
else
success= BKE_read_file_from_memfile(&uel->memfile);
/* restore */
strcpy(G.sce, scestr);
G.fileflags= fileflags;
return success;
}
/* name can be a dynamic string */
void BKE_write_undo(char *name)
{
int nr, success;
UndoElem *uel;
if( (U.uiflag & USER_GLOBALUNDO)==0) return;
/* remove all undos after (also when curundo==NULL) */
while(undobase.last != curundo) {
uel= undobase.last;
BLI_remlink(&undobase, uel);
BLO_free_memfile(&uel->memfile);
MEM_freeN(uel);
}
/* make new */
curundo= uel= MEM_callocN(sizeof(UndoElem), "undo file");
strncpy(uel->name, name, MAXUNDONAME-1);
BLI_addtail(&undobase, uel);
/* and limit amount to the maximum */
nr= 0;
uel= undobase.last;
while(uel) {
nr++;
if(nr==MAXUNDO) break;
uel= uel->prev;
}
if(uel) {
while(undobase.first!=uel) {
UndoElem *first= undobase.first;
BLI_remlink(&undobase, first);
/* the merge is because of compression */
BLO_merge_memfile(&first->memfile, &first->next->memfile);
MEM_freeN(first);
}
}
/* disk save version */
if(UNDO_DISK) {
static int counter= 0;
char *err, tstr[FILE_MAXDIR+FILE_MAXFILE];
char numstr[32];
/* calculate current filename */
counter++;
counter= counter % MAXUNDO;
sprintf(numstr, "%d.blend", counter);
BLI_make_file_string("/", tstr, U.tempdir, numstr);
success= BLO_write_file(tstr, G.fileflags, &err);
strcpy(curundo->str, tstr);
}
else {
MemFile *prevfile=NULL;
char *err;
if(curundo->prev) prevfile= &(curundo->prev->memfile);
success= BLO_write_file_mem(prevfile, &curundo->memfile, G.fileflags, &err);
}
}
/* 1= an undo, -1 is a redo. we have to make sure 'curundo' remains at current situation */
void BKE_undo_step(int step)
{
if(step==1) {
/* curundo should never be NULL, after restart or load file it should call undo_save */
if(curundo==NULL || curundo->prev==NULL) error("No undo available");
else {
printf("undo %s\n", curundo->name);
curundo= curundo->prev;
read_undosave(curundo);
}
}
else {
/* curundo has to remain current situation! */
if(curundo==NULL || curundo->next==NULL) error("No redo available");
else {
read_undosave(curundo->next);
curundo= curundo->next;
printf("redo %s\n", curundo->name);
}
}
}
void BKE_reset_undo(void)
{
UndoElem *uel;
uel= undobase.first;
while(uel) {
BLO_free_memfile(&uel->memfile);
uel= uel->next;
}
BLI_freelistN(&undobase);
curundo= NULL;
}
void BKE_undo_menu(void)
{
}
/* saves quit.blend */
void BKE_undo_save_quit(void)
{
UndoElem *uel;
MemFileChunk *chunk;
int file;
char str[FILE_MAXDIR+FILE_MAXFILE];
if( (U.uiflag & USER_GLOBALUNDO)==0) return;
uel= curundo;
if(uel==NULL) {
printf("No undo buffer to save recovery file\n");
return;
}
/* no undo state to save */
if(undobase.first==undobase.last) return;
BLI_make_file_string("/", str, U.tempdir, "quit.blend");
file = open(str,O_BINARY+O_WRONLY+O_CREAT+O_TRUNC, 0666);
if(file == -1) {
printf("Unable to save %s\n", str);
return;
}
chunk= uel->memfile.chunks.first;
while(chunk) {
if( write(file, chunk->buf, chunk->size) != chunk->size) break;
chunk= chunk->next;
}
close(file);
if(chunk) printf("Unable to save %s\n", str);
else printf("Saved session recovery to %s\n", str);
}