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 * Contributor(s): Blender Foundation 2007
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/windowmanager/intern/wm_files.c
33 /* placed up here because of crappy
40 #include "zlib.h" /* wm_read_exotic() */
43 #include <windows.h> /* need to include windows.h so _WIN32_IE is defined */
45 #define _WIN32_IE 0x0400 /* minimal requirements for SHGetSpecialFolderPath on MINGW MSVC has this defined already */
47 #include <shlobj.h> /* for SHGetSpecialFolderPath, has to be done before BLI_winstuff because 'near' is disabled through BLI_windstuff */
48 #include <process.h> /* getpid */
49 #include "BLI_winstuff.h"
51 #include <unistd.h> /* getpid */
54 #include "MEM_guardedalloc.h"
55 #include "MEM_CacheLimiterC-Api.h"
57 #include "BLI_blenlib.h"
58 #include "BLI_linklist.h"
59 #include "BLI_utildefines.h"
60 #include "BLI_callbacks.h"
62 #include "BLF_translation.h"
64 #include "DNA_anim_types.h"
65 #include "DNA_ipo_types.h" // XXX old animation system
66 #include "DNA_object_types.h"
67 #include "DNA_space_types.h"
68 #include "DNA_userdef_types.h"
69 #include "DNA_scene_types.h"
70 #include "DNA_screen_types.h"
71 #include "DNA_windowmanager_types.h"
73 #include "BKE_blender.h"
74 #include "BKE_context.h"
75 #include "BKE_depsgraph.h"
76 #include "BKE_DerivedMesh.h"
78 #include "BKE_global.h"
79 #include "BKE_library.h"
81 #include "BKE_packedFile.h"
82 #include "BKE_report.h"
83 #include "BKE_sound.h"
84 #include "BKE_texture.h"
87 #include "BLO_readfile.h"
88 #include "BLO_writefile.h"
90 #include "RNA_access.h"
92 #include "IMB_imbuf.h"
93 #include "IMB_imbuf_types.h"
94 #include "IMB_thumbs.h"
96 #include "ED_datafiles.h"
97 #include "ED_object.h"
98 #include "ED_screen.h"
99 #include "ED_sculpt.h"
100 #include "ED_view3d.h"
103 #include "RE_pipeline.h" /* only to report missing engine */
105 #include "GHOST_C-api.h"
106 #include "GHOST_Path-api.h"
108 #include "UI_interface.h"
110 #include "GPU_draw.h"
113 #include "BPY_extern.h"
117 #include "WM_types.h"
119 #include "wm_files.h"
120 #include "wm_window.h"
121 #include "wm_event_system.h"
123 static void write_history(void);
125 /* To be able to read files without windows closing, opening, moving
126 we try to prepare for worst case:
127 - active window gets active screen from file
128 - restoring the screens from non-active windows
129 Best case is all screens match, in that case they get assigned to proper window
131 static void wm_window_match_init(bContext *C, ListBase *wmlist)
134 wmWindow *win, *active_win;
137 G.main->wm.first= G.main->wm.last= NULL;
139 active_win = CTX_wm_window(C);
141 /* first wrap up running stuff */
142 /* code copied from wm_init_exit.c */
143 for(wm= wmlist->first; wm; wm= wm->id.next) {
145 WM_jobs_stop_all(wm);
147 for(win= wm->windows.first; win; win= win->next) {
149 CTX_wm_window_set(C, win); /* needed by operator close callbacks */
150 WM_event_remove_handlers(C, &win->handlers);
151 WM_event_remove_handlers(C, &win->modalhandlers);
152 ED_screen_exit(C, win, win->screen);
156 /* reset active window */
157 CTX_wm_window_set(C, active_win);
163 if(G.fileflags & G_FILE_NO_UI) return;
165 /* we take apart the used screens from non-active window */
166 for(win= wm->windows.first; win; win= win->next) {
167 BLI_strncpy(win->screenname, win->screen->id.name, MAX_ID_NAME);
168 if(win!=wm->winactive) {
169 BLI_remlink(&G.main->screen, win->screen);
170 //BLI_addtail(screenbase, win->screen);
175 /* match old WM with new, 4 cases:
176 1- no current wm, no read wm: make new default
177 2- no current wm, but read wm: that's OK, do nothing
178 3- current wm, but not in file: try match screen names
179 4- current wm, and wm in file: try match ghostwin
182 static void wm_window_match_do(bContext *C, ListBase *oldwmlist)
184 wmWindowManager *oldwm, *wm;
185 wmWindow *oldwin, *win;
188 if(oldwmlist->first==NULL) {
189 if(G.main->wm.first); /* nothing todo */
196 /* we've read file without wm..., keep current one entirely alive */
197 if(G.main->wm.first==NULL) {
198 /* when loading without UI, no matching needed */
199 if(!(G.fileflags & G_FILE_NO_UI)) {
200 bScreen *screen= CTX_wm_screen(C);
202 /* match oldwm to new dbase, only old files */
203 for(wm= oldwmlist->first; wm; wm= wm->id.next) {
205 for(win= wm->windows.first; win; win= win->next) {
206 /* all windows get active screen from file */
210 win->screen= ED_screen_duplicate(win, screen);
212 BLI_strncpy(win->screenname, win->screen->id.name+2, sizeof(win->screenname));
213 win->screen->winid= win->winid;
218 G.main->wm= *oldwmlist;
220 /* screens were read from file! */
221 ED_screens_initialize(G.main->wm.first);
224 /* what if old was 3, and loaded 1? */
225 /* this code could move to setup_appdata */
226 oldwm= oldwmlist->first;
227 wm= G.main->wm.first;
229 /* preserve key configurations in new wm, to preserve their keymaps */
230 wm->keyconfigs= oldwm->keyconfigs;
231 wm->addonconf= oldwm->addonconf;
232 wm->defaultconf= oldwm->defaultconf;
233 wm->userconf= oldwm->userconf;
235 oldwm->keyconfigs.first= oldwm->keyconfigs.last= NULL;
236 oldwm->addonconf= NULL;
237 oldwm->defaultconf= NULL;
238 oldwm->userconf= NULL;
240 /* ensure making new keymaps and set space types */
244 /* only first wm in list has ghostwins */
245 for(win= wm->windows.first; win; win= win->next) {
246 for(oldwin= oldwm->windows.first; oldwin; oldwin= oldwin->next) {
248 if(oldwin->winid == win->winid ) {
249 win->ghostwin= oldwin->ghostwin;
250 win->active= oldwin->active;
254 if(!G.background) /* file loading in background mode still calls this */
255 GHOST_SetWindowUserData(win->ghostwin, win); /* pointer back */
257 oldwin->ghostwin= NULL;
259 win->eventstate= oldwin->eventstate;
260 oldwin->eventstate= NULL;
262 /* ensure proper screen rescaling */
263 win->sizex= oldwin->sizex;
264 win->sizey= oldwin->sizey;
265 win->posx= oldwin->posx;
266 win->posy= oldwin->posy;
270 wm_close_and_free_all(C, oldwmlist);
275 /* in case UserDef was read, we re-initialize all, and do versioning */
276 static void wm_init_userdef(bContext *C)
279 MEM_CacheLimiter_set_maximum(U.memcachelimit * 1024 * 1024);
280 sound_init(CTX_data_main(C));
282 /* needed so loading a file from the command line respects user-pref [#26156] */
283 if(U.flag & USER_FILENOUI) G.fileflags |= G_FILE_NO_UI;
284 else G.fileflags &= ~G_FILE_NO_UI;
286 /* set the python auto-execute setting from user prefs */
287 /* enabled by default, unless explicitly enabled in the command line which overrides */
288 if((G.f & G_SCRIPT_OVERRIDE_PREF) == 0) {
289 if ((U.flag & USER_SCRIPT_AUTOEXEC_DISABLE) == 0) G.f |= G_SCRIPT_AUTOEXEC;
290 else G.f &= ~G_SCRIPT_AUTOEXEC;
292 /* update tempdir from user preferences */
293 BLI_where_is_temp(btempdir, FILE_MAX, 1);
299 #define BKE_READ_EXOTIC_FAIL_PATH -3 /* file format is not supported */
300 #define BKE_READ_EXOTIC_FAIL_FORMAT -2 /* file format is not supported */
301 #define BKE_READ_EXOTIC_FAIL_OPEN -1 /* Can't open the file */
302 #define BKE_READ_EXOTIC_OK_BLEND 0 /* .blend file */
303 #define BKE_READ_EXOTIC_OK_OTHER 1 /* other supported formats */
305 /* intended to check for non-blender formats but for now it only reads blends */
306 static int wm_read_exotic(Scene *UNUSED(scene), const char *name)
313 // make sure we're not trying to read a directory....
316 if (ELEM(name[len-1], '/', '\\')) {
317 retval= BKE_READ_EXOTIC_FAIL_PATH;
320 gzfile = gzopen(name,"rb");
322 if (gzfile == NULL) {
323 retval= BKE_READ_EXOTIC_FAIL_OPEN;
326 len= gzread(gzfile, header, sizeof(header));
328 if (len == sizeof(header) && strncmp(header, "BLENDER", 7) == 0) {
329 retval= BKE_READ_EXOTIC_OK_BLEND;
334 if(is_foo_format(name)) {
336 retval= BKE_READ_EXOTIC_OK_OTHER;
341 retval= BKE_READ_EXOTIC_FAIL_FORMAT;
351 void WM_read_file(bContext *C, const char *filepath, ReportList *reports)
355 /* so we can get the error message */
360 BLI_exec_cb(CTX_data_main(C), NULL, BLI_CB_EVT_LOAD_PRE);
362 /* first try to append data from exotic file formats... */
363 /* it throws error box when file doesnt exist and returns -1 */
364 /* note; it should set some error message somewhere... (ton) */
365 retval= wm_read_exotic(CTX_data_scene(C), filepath);
367 /* we didn't succeed, now try to read Blender file */
368 if (retval == BKE_READ_EXOTIC_OK_BLEND) {
372 /* put aside screens to match with persistant windows later */
373 /* also exit screens and editors */
374 wm_window_match_init(C, &wmbase);
376 retval= BKE_read_file(C, filepath, reports);
379 /* this flag is initialized by the operator but overwritten on read.
380 * need to re-enable it here else drivers + registered scripts wont work. */
382 const int flags_keep= (G_SCRIPT_AUTOEXEC | G_SCRIPT_OVERRIDE_PREF);
383 G.f= (G.f & ~flags_keep) | (G_f & flags_keep);
386 /* match the read WM with current WM */
387 wm_window_match_do(C, &wmbase);
388 WM_check(C); /* opens window(s), checks keymaps */
390 // XXX mainwindow_set_filename_to_title(G.main->name);
392 if(retval == BKE_READ_FILE_OK_USERPREFS) wm_init_userdef(C); // in case a userdef is read from regular .blend
394 if (retval != BKE_READ_FILE_FAIL) {
396 if(!G.background) /* assume automated tasks with background, dont write recent file list */
401 WM_event_add_notifier(C, NC_WM|ND_FILEREAD, NULL);
402 // refresh_interface_font();
404 CTX_wm_window_set(C, CTX_wm_manager(C)->windows.first);
407 DAG_on_visible_update(CTX_data_main(C), TRUE);
410 /* run any texts that were loaded in and flagged as modules */
412 BPY_app_handlers_reset();
413 BPY_modules_load_user(C);
416 /* important to do before NULL'ing the context */
417 BLI_exec_cb(CTX_data_main(C), NULL, BLI_CB_EVT_LOAD_POST);
419 CTX_wm_window_set(C, NULL); /* exits queues */
421 #if 0 /* gives popups on windows but not linux, bug in report API but disable for now to stop users getting annoyed */
422 /* TODO, make this show in header info window */
425 for(sce= G.main->scene.first; sce; sce= sce->id.next) {
426 if(sce->r.engine[0] && BLI_findstring(&R_engines, sce->r.engine, offsetof(RenderEngineType, idname)) == NULL) {
427 BKE_reportf(reports, RPT_WARNING, "Engine not available: '%s' for scene: %s, an addon may need to be installed or enabled", sce->r.engine, sce->id.name+2);
433 // XXX undo_editmode_clear();
435 BKE_write_undo(C, "original"); /* save current state */
437 else if(retval == BKE_READ_EXOTIC_OK_OTHER)
438 BKE_write_undo(C, "Import file");
439 else if(retval == BKE_READ_EXOTIC_FAIL_OPEN) {
440 BKE_reportf(reports, RPT_ERROR, IFACE_("Can't read file: \"%s\", %s."), filepath,
441 errno ? strerror(errno) : IFACE_("Unable to open the file"));
443 else if(retval == BKE_READ_EXOTIC_FAIL_FORMAT) {
444 BKE_reportf(reports, RPT_ERROR, IFACE_("File format is not supported in file: \"%s\"."), filepath);
446 else if(retval == BKE_READ_EXOTIC_FAIL_PATH) {
447 BKE_reportf(reports, RPT_ERROR, IFACE_("File path invalid: \"%s\"."), filepath);
450 BKE_reportf(reports, RPT_ERROR, IFACE_("Unknown error loading: \"%s\"."), filepath);
451 BLI_assert(!"invalid 'retval'");
459 /* called on startup, (context entirely filled with NULLs) */
460 /* or called for 'New File' */
462 int WM_read_homefile(bContext *C, ReportList *UNUSED(reports), short from_memory)
465 char tstr[FILE_MAXDIR+FILE_MAXFILE];
468 free_ttfont(); /* still weird... what does it here? */
472 char *cfgdir = BLI_get_folder(BLENDER_USER_CONFIG, NULL);
474 BLI_make_file_string(G.main->name, tstr, cfgdir, BLENDER_STARTUP_FILE);
481 /* prevent loading no UI */
482 G.fileflags &= ~G_FILE_NO_UI;
484 /* put aside screens to match with persistant windows later */
485 wm_window_match_init(C, &wmbase);
487 if (!from_memory && BLI_exists(tstr)) {
488 success = (BKE_read_file(C, tstr, NULL) != BKE_READ_FILE_FAIL);
490 if(U.themes.first==NULL) {
491 printf("\nError: No valid "STRINGIFY(BLENDER_STARTUP_FILE)", fall back to built-in default.\n\n");
496 success = BKE_read_file_from_memory(C, datatoc_startup_blend, datatoc_startup_blend_size, NULL);
497 if (wmbase.first == NULL) wm_clear_default_size(C);
499 #ifdef WITH_PYTHON_SECURITY /* not default */
500 /* use alternative setting for security nuts
501 * otherwise we'd need to patch the binary blob - startup.blend.c */
502 U.flag |= USER_SCRIPT_AUTOEXEC_DISABLE;
506 /* prevent buggy files that had G_FILE_RELATIVE_REMAP written out by mistake. Screws up autosaves otherwise
507 * can remove this eventually, only in a 2.53 and older, now its not written */
508 G.fileflags &= ~G_FILE_RELATIVE_REMAP;
510 /* check userdef before open window, keymaps etc */
513 /* match the read WM with current WM */
514 wm_window_match_do(C, &wmbase);
515 WM_check(C); /* opens window(s), checks keymaps */
517 G.main->name[0]= '\0';
519 /* When loading factory settings, the reset solid OpenGL lights need to be applied. */
520 if (!G.background) GPU_default_lights();
523 G.save_over = 0; // start with save preference untitled.blend
524 G.fileflags &= ~G_FILE_AUTOPLAY; /* disable autoplay in startup.blend... */
525 // mainwindow_set_filename_to_title(""); // empty string re-initializes title to "Blender"
527 // refresh_interface_font();
529 // undo_editmode_clear();
531 BKE_write_undo(C, "original"); /* save current state */
534 DAG_on_visible_update(CTX_data_main(C), TRUE);
537 if(CTX_py_init_get(C)) {
538 /* sync addons, these may have changed from the defaults */
539 BPY_string_exec(C, "__import__('addon_utils').reset_all()");
542 BPY_app_handlers_reset();
543 BPY_modules_load_user(C);
547 WM_event_add_notifier(C, NC_WM|ND_FILEREAD, NULL);
549 /* in background mode the scene will stay NULL */
551 CTX_wm_window_set(C, NULL); /* exits queues */
557 int WM_read_homefile_exec(bContext *C, wmOperator *op)
559 int from_memory= strcmp(op->type->idname, "WM_OT_read_factory_settings") == 0;
560 return WM_read_homefile(C, op->reports, from_memory) ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
563 void WM_read_history(void)
567 struct RecentFile *recent;
570 char *cfgdir = BLI_get_folder(BLENDER_USER_CONFIG, NULL);
574 BLI_make_file_string("/", name, cfgdir, BLENDER_HISTORY_FILE);
576 lines= BLI_read_file_as_lines(name);
578 G.recent_files.first = G.recent_files.last = NULL;
580 /* read list of recent opend files from recent-files.txt to memory */
581 for (l= lines, num= 0; l && (num<U.recent_files); l= l->next) {
583 if (line[0] && BLI_exists(line)) {
584 recent = (RecentFile*)MEM_mallocN(sizeof(RecentFile),"RecentFile");
585 BLI_addtail(&(G.recent_files), recent);
586 recent->filepath = BLI_strdup(line);
591 BLI_free_file_lines(lines);
595 static void write_history(void)
597 struct RecentFile *recent, *next_recent;
598 char name[FILE_MAXDIR+FILE_MAXFILE];
599 char *user_config_dir;
603 /* will be NULL in background mode */
604 user_config_dir = BLI_get_folder_create(BLENDER_USER_CONFIG, NULL);
608 BLI_make_file_string("/", name, user_config_dir, BLENDER_HISTORY_FILE);
610 recent = G.recent_files.first;
611 /* refresh recent-files.txt of recent opened files, when current file was changed */
612 if(!(recent) || (BLI_path_cmp(recent->filepath, G.main->name)!=0)) {
613 fp= fopen(name, "w");
615 /* add current file to the beginning of list */
616 recent = (RecentFile*)MEM_mallocN(sizeof(RecentFile),"RecentFile");
617 recent->filepath = BLI_strdup(G.main->name);
618 BLI_addhead(&(G.recent_files), recent);
619 /* write current file to recent-files.txt */
620 fprintf(fp, "%s\n", recent->filepath);
621 recent = recent->next;
623 /* write rest of recent opened files to recent-files.txt */
624 while((i<U.recent_files) && (recent)){
625 /* this prevents to have duplicities in list */
626 if (BLI_path_cmp(recent->filepath, G.main->name)!=0) {
627 fprintf(fp, "%s\n", recent->filepath);
628 recent = recent->next;
631 next_recent = recent->next;
632 MEM_freeN(recent->filepath);
633 BLI_freelinkN(&(G.recent_files), recent);
634 recent = next_recent;
641 /* also update most recent files on System */
642 GHOST_addToSystemRecentFiles(G.main->name);
646 static ImBuf *blend_file_thumb(Scene *scene, int **thumb_pt)
648 /* will be scaled down, but gives some nice oversampling */
651 char err_out[256]= "unknown";
655 /* scene can be NULL if running a script at startup and calling the save operator */
656 if(G.background || scene==NULL || scene->camera==NULL)
659 /* gets scaled to BLEN_THUMB_SIZE */
660 ibuf= ED_view3d_draw_offscreen_imbuf_simple(scene, scene->camera, BLEN_THUMB_SIZE * 2, BLEN_THUMB_SIZE * 2, IB_rect, OB_SOLID, err_out);
663 float aspect= (scene->r.xsch*scene->r.xasp) / (scene->r.ysch*scene->r.yasp);
665 /* dirty oversampling */
666 IMB_scaleImBuf(ibuf, BLEN_THUMB_SIZE, BLEN_THUMB_SIZE);
668 /* add pretty overlay */
669 IMB_overlayblend_thumb(ibuf->rect, ibuf->x, ibuf->y, aspect);
671 /* first write into thumb buffer */
672 thumb= MEM_mallocN(((2 + (BLEN_THUMB_SIZE * BLEN_THUMB_SIZE))) * sizeof(int), "write_file thumb");
674 thumb[0] = BLEN_THUMB_SIZE;
675 thumb[1] = BLEN_THUMB_SIZE;
677 memcpy(thumb + 2, ibuf->rect, BLEN_THUMB_SIZE * BLEN_THUMB_SIZE * sizeof(int));
680 /* '*thumb_pt' needs to stay NULL to prevent a bad thumbnail from being handled */
681 fprintf(stderr, "blend_file_thumb failed to create thumbnail: %s\n", err_out);
685 /* must be freed by caller */
691 /* easy access from gdb */
692 int write_crash_blend(void)
695 int fileflags = G.fileflags & ~(G_FILE_HISTORY); /* don't do file history on crash file */
697 BLI_strncpy(path, G.main->name, sizeof(path));
698 BLI_replace_extension(path, sizeof(path), "_crash.blend");
699 if(BLO_write_file(G.main, path, fileflags, NULL, NULL)) {
700 printf("written: %s\n", path);
704 printf("failed: %s\n", path);
709 int WM_write_file(bContext *C, const char *target, int fileflags, ReportList *reports, int copy)
713 char filepath[FILE_MAX];
716 ImBuf *ibuf_thumb= NULL;
718 len = strlen(target);
721 BKE_report(reports, RPT_ERROR, "Path is empty, cannot save");
725 if (len >= FILE_MAX) {
726 BKE_report(reports, RPT_ERROR, "Path too long, cannot save");
730 BLI_strncpy(filepath, target, FILE_MAX);
731 BLI_replace_extension(filepath, FILE_MAX, ".blend");
732 /* dont use 'target' anymore */
734 /* send the OnSave event */
735 for (li= G.main->library.first; li; li= li->id.next) {
736 if (BLI_path_cmp(li->filepath, filepath) == 0) {
737 BKE_reportf(reports, RPT_ERROR, "Can't overwrite used library '%.200s'", filepath);
742 /* blend file thumbnail */
743 /* save before exit_editmode, otherwise derivedmeshes for shared data corrupt #27765) */
744 if(U.flag & USER_SAVE_PREVIEWS) {
745 ibuf_thumb= blend_file_thumb(CTX_data_scene(C), &thumb);
748 BLI_exec_cb(G.main, NULL, BLI_CB_EVT_SAVE_PRE);
750 /* operator now handles overwrite checks */
752 if (G.fileflags & G_AUTOPACK) {
753 packAll(G.main, reports);
756 ED_object_exit_editmode(C, EM_DO_UNDO);
757 ED_sculpt_force_update(C);
759 /* don't forget not to return without! */
762 fileflags |= G_FILE_HISTORY; /* write file history */
764 if (BLO_write_file(CTX_data_main(C), filepath, fileflags, reports, thumb)) {
767 BLI_strncpy(G.main->name, filepath, sizeof(G.main->name)); /* is guaranteed current file */
769 G.save_over = 1; /* disable untitled.blend convention */
772 if(fileflags & G_FILE_COMPRESS) G.fileflags |= G_FILE_COMPRESS;
773 else G.fileflags &= ~G_FILE_COMPRESS;
775 if(fileflags & G_FILE_AUTOPLAY) G.fileflags |= G_FILE_AUTOPLAY;
776 else G.fileflags &= ~G_FILE_AUTOPLAY;
778 /* prevent background mode scripts from clobbering history */
783 BLI_exec_cb(G.main, NULL, BLI_CB_EVT_SAVE_POST);
785 /* run this function after because the file cant be written before the blend is */
787 ibuf_thumb= IMB_thumb_create(filepath, THB_NORMAL, THB_SOURCE_BLEND, ibuf_thumb);
788 IMB_freeImBuf(ibuf_thumb);
791 if(thumb) MEM_freeN(thumb);
794 if(ibuf_thumb) IMB_freeImBuf(ibuf_thumb);
795 if(thumb) MEM_freeN(thumb);
807 int WM_write_homefile(bContext *C, wmOperator *op)
809 wmWindowManager *wm= CTX_wm_manager(C);
810 wmWindow *win= CTX_wm_window(C);
811 char filepath[FILE_MAXDIR+FILE_MAXFILE];
814 /* check current window and close it if temp */
815 if(win->screen->temp)
816 wm_window_close(C, wm, win);
818 /* update keymaps in user preferences */
819 WM_keyconfig_update(wm);
821 BLI_make_file_string("/", filepath, BLI_get_folder_create(BLENDER_USER_CONFIG, NULL), BLENDER_STARTUP_FILE);
822 printf("trying to save homefile at %s ", filepath);
824 /* force save as regular blend file */
825 fileflags = G.fileflags & ~(G_FILE_COMPRESS | G_FILE_AUTOPLAY | G_FILE_LOCK | G_FILE_SIGN | G_FILE_HISTORY);
827 if(BLO_write_file(CTX_data_main(C), filepath, fileflags, op->reports, NULL) == 0) {
829 return OPERATOR_CANCELLED;
836 return OPERATOR_FINISHED;
839 /************************ autosave ****************************/
841 void wm_autosave_location(char *filepath)
848 BLI_snprintf(pidstr, sizeof(pidstr), "%d.blend", abs(getpid()));
851 /* XXX Need to investigate how to handle default location of '/tmp/'
852 * This is a relative directory on Windows, and it may be
854 * Blender installed on D:\ drive, D:\ drive has D:\tmp\
855 * Now, BLI_exists() will find '/tmp/' exists, but
856 * BLI_make_file_string will create string that has it most likely on C:\
857 * through get_default_root().
858 * If there is no C:\tmp autosave fails. */
859 if (!BLI_exists(btempdir)) {
860 savedir = BLI_get_folder_create(BLENDER_USER_AUTOSAVE, NULL);
861 BLI_make_file_string("/", filepath, savedir, pidstr);
866 BLI_make_file_string("/", filepath, btempdir, pidstr);
869 void WM_autosave_init(wmWindowManager *wm)
871 wm_autosave_timer_ended(wm);
873 if(U.flag & USER_AUTOSAVE)
874 wm->autosavetimer= WM_event_add_timer(wm, NULL, TIMERAUTOSAVE, U.savetime*60.0);
877 void wm_autosave_timer(const bContext *C, wmWindowManager *wm, wmTimer *UNUSED(wt))
880 wmEventHandler *handler;
881 char filepath[FILE_MAX];
884 WM_event_remove_timer(wm, NULL, wm->autosavetimer);
886 /* if a modal operator is running, don't autosave, but try again in 10 seconds */
887 for(win=wm->windows.first; win; win=win->next) {
888 for(handler=win->modalhandlers.first; handler; handler=handler->next) {
890 wm->autosavetimer= WM_event_add_timer(wm, NULL, TIMERAUTOSAVE, 10.0);
896 wm_autosave_location(filepath);
898 /* force save as regular blend file */
899 fileflags = G.fileflags & ~(G_FILE_COMPRESS|G_FILE_AUTOPLAY |G_FILE_LOCK|G_FILE_SIGN|G_FILE_HISTORY);
901 /* no error reporting to console */
902 BLO_write_file(CTX_data_main(C), filepath, fileflags, NULL, NULL);
904 /* do timer after file write, just in case file write takes a long time */
905 wm->autosavetimer= WM_event_add_timer(wm, NULL, TIMERAUTOSAVE, U.savetime*60.0);
908 void wm_autosave_timer_ended(wmWindowManager *wm)
910 if(wm->autosavetimer) {
911 WM_event_remove_timer(wm, NULL, wm->autosavetimer);
912 wm->autosavetimer= NULL;
916 void wm_autosave_delete(void)
918 char filename[FILE_MAX];
920 wm_autosave_location(filename);
922 if(BLI_exists(filename)) {
923 char str[FILE_MAXDIR+FILE_MAXFILE];
924 BLI_make_file_string("/", str, btempdir, "quit.blend");
926 /* if global undo; remove tempsave, otherwise rename */
927 if(U.uiflag & USER_GLOBALUNDO) BLI_delete(filename, 0, 0);
928 else BLI_rename(filename, str);
932 void wm_autosave_read(bContext *C, ReportList *reports)
934 char filename[FILE_MAX];
936 wm_autosave_location(filename);
937 WM_read_file(C, filename, reports);