|
Blender
V2.59
|
00001 /* 00002 * $Id: wm_files.c 39084 2011-08-05 20:45:26Z blendix $ 00003 * 00004 * ***** BEGIN GPL LICENSE BLOCK ***** 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software Foundation, 00018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 * 00020 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. 00021 * All rights reserved. 00022 * 00023 * Contributor(s): Blender Foundation 2007 00024 * 00025 * ***** END GPL LICENSE BLOCK ***** 00026 */ 00027 00033 /* placed up here because of crappy 00034 * winsock stuff. 00035 */ 00036 #include <stddef.h> 00037 #include <string.h> 00038 #include <errno.h> 00039 00040 #include "zlib.h" /* wm_read_exotic() */ 00041 00042 #ifdef WIN32 00043 #include <windows.h> /* need to include windows.h so _WIN32_IE is defined */ 00044 #ifndef _WIN32_IE 00045 #define _WIN32_IE 0x0400 /* minimal requirements for SHGetSpecialFolderPath on MINGW MSVC has this defined already */ 00046 #endif 00047 #include <shlobj.h> /* for SHGetSpecialFolderPath, has to be done before BLI_winstuff because 'near' is disabled through BLI_windstuff */ 00048 #include <process.h> /* getpid */ 00049 #include "BLI_winstuff.h" 00050 #else 00051 #include <unistd.h> /* getpid */ 00052 #endif 00053 00054 #include "MEM_guardedalloc.h" 00055 #include "MEM_CacheLimiterC-Api.h" 00056 00057 #include "BLI_blenlib.h" 00058 #include "BLI_linklist.h" 00059 #include "BLI_utildefines.h" 00060 #include "BLI_callbacks.h" 00061 00062 #include "DNA_anim_types.h" 00063 #include "DNA_ipo_types.h" // XXX old animation system 00064 #include "DNA_object_types.h" 00065 #include "DNA_space_types.h" 00066 #include "DNA_userdef_types.h" 00067 #include "DNA_scene_types.h" 00068 #include "DNA_screen_types.h" 00069 #include "DNA_windowmanager_types.h" 00070 00071 #include "BKE_blender.h" 00072 #include "BKE_context.h" 00073 #include "BKE_depsgraph.h" 00074 #include "BKE_DerivedMesh.h" 00075 #include "BKE_font.h" 00076 #include "BKE_global.h" 00077 #include "BKE_library.h" 00078 #include "BKE_main.h" 00079 #include "BKE_packedFile.h" 00080 #include "BKE_report.h" 00081 #include "BKE_sound.h" 00082 #include "BKE_texture.h" 00083 00084 00085 #include "BLO_readfile.h" 00086 #include "BLO_writefile.h" 00087 00088 #include "RNA_access.h" 00089 00090 #include "IMB_imbuf.h" 00091 #include "IMB_imbuf_types.h" 00092 #include "IMB_thumbs.h" 00093 00094 #include "ED_datafiles.h" 00095 #include "ED_object.h" 00096 #include "ED_screen.h" 00097 #include "ED_sculpt.h" 00098 #include "ED_view3d.h" 00099 #include "ED_util.h" 00100 00101 #include "RE_pipeline.h" /* only to report missing engine */ 00102 00103 #include "GHOST_C-api.h" 00104 #include "GHOST_Path-api.h" 00105 00106 #include "UI_interface.h" 00107 00108 #include "GPU_draw.h" 00109 00110 #ifdef WITH_PYTHON 00111 #include "BPY_extern.h" 00112 #endif 00113 00114 #include "WM_api.h" 00115 #include "WM_types.h" 00116 #include "wm.h" 00117 #include "wm_files.h" 00118 #include "wm_window.h" 00119 #include "wm_event_system.h" 00120 00121 static void write_history(void); 00122 00123 /* To be able to read files without windows closing, opening, moving 00124 we try to prepare for worst case: 00125 - active window gets active screen from file 00126 - restoring the screens from non-active windows 00127 Best case is all screens match, in that case they get assigned to proper window 00128 */ 00129 static void wm_window_match_init(bContext *C, ListBase *wmlist) 00130 { 00131 wmWindowManager *wm; 00132 wmWindow *win, *active_win; 00133 00134 *wmlist= G.main->wm; 00135 G.main->wm.first= G.main->wm.last= NULL; 00136 00137 active_win = CTX_wm_window(C); 00138 00139 /* first wrap up running stuff */ 00140 /* code copied from wm_init_exit.c */ 00141 for(wm= wmlist->first; wm; wm= wm->id.next) { 00142 00143 WM_jobs_stop_all(wm); 00144 00145 for(win= wm->windows.first; win; win= win->next) { 00146 00147 CTX_wm_window_set(C, win); /* needed by operator close callbacks */ 00148 WM_event_remove_handlers(C, &win->handlers); 00149 WM_event_remove_handlers(C, &win->modalhandlers); 00150 ED_screen_exit(C, win, win->screen); 00151 } 00152 } 00153 00154 /* reset active window */ 00155 CTX_wm_window_set(C, active_win); 00156 00157 ED_editors_exit(C); 00158 00159 return; 00160 if(wm==NULL) return; 00161 if(G.fileflags & G_FILE_NO_UI) return; 00162 00163 /* we take apart the used screens from non-active window */ 00164 for(win= wm->windows.first; win; win= win->next) { 00165 BLI_strncpy(win->screenname, win->screen->id.name, MAX_ID_NAME); 00166 if(win!=wm->winactive) { 00167 BLI_remlink(&G.main->screen, win->screen); 00168 //BLI_addtail(screenbase, win->screen); 00169 } 00170 } 00171 } 00172 00173 /* match old WM with new, 4 cases: 00174 1- no current wm, no read wm: make new default 00175 2- no current wm, but read wm: that's OK, do nothing 00176 3- current wm, but not in file: try match screen names 00177 4- current wm, and wm in file: try match ghostwin 00178 */ 00179 00180 static void wm_window_match_do(bContext *C, ListBase *oldwmlist) 00181 { 00182 wmWindowManager *oldwm, *wm; 00183 wmWindow *oldwin, *win; 00184 00185 /* cases 1 and 2 */ 00186 if(oldwmlist->first==NULL) { 00187 if(G.main->wm.first); /* nothing todo */ 00188 else 00189 wm_add_default(C); 00190 } 00191 else { 00192 /* cases 3 and 4 */ 00193 00194 /* we've read file without wm..., keep current one entirely alive */ 00195 if(G.main->wm.first==NULL) { 00196 /* when loading without UI, no matching needed */ 00197 if(!(G.fileflags & G_FILE_NO_UI)) { 00198 bScreen *screen= CTX_wm_screen(C); 00199 00200 /* match oldwm to new dbase, only old files */ 00201 for(wm= oldwmlist->first; wm; wm= wm->id.next) { 00202 00203 for(win= wm->windows.first; win; win= win->next) { 00204 /* all windows get active screen from file */ 00205 if(screen->winid==0) 00206 win->screen= screen; 00207 else 00208 win->screen= ED_screen_duplicate(win, screen); 00209 00210 BLI_strncpy(win->screenname, win->screen->id.name+2, sizeof(win->screenname)); 00211 win->screen->winid= win->winid; 00212 } 00213 } 00214 } 00215 00216 G.main->wm= *oldwmlist; 00217 00218 /* screens were read from file! */ 00219 ED_screens_initialize(G.main->wm.first); 00220 } 00221 else { 00222 /* what if old was 3, and loaded 1? */ 00223 /* this code could move to setup_appdata */ 00224 oldwm= oldwmlist->first; 00225 wm= G.main->wm.first; 00226 00227 /* move addon key configuration to new wm, to preserve their keymaps */ 00228 if(oldwm->addonconf) { 00229 wm->addonconf= oldwm->addonconf; 00230 BLI_remlink(&oldwm->keyconfigs, oldwm->addonconf); 00231 oldwm->addonconf= NULL; 00232 BLI_addtail(&wm->keyconfigs, wm->addonconf); 00233 } 00234 00235 /* ensure making new keymaps and set space types */ 00236 wm->initialized= 0; 00237 wm->winactive= NULL; 00238 00239 /* only first wm in list has ghostwins */ 00240 for(win= wm->windows.first; win; win= win->next) { 00241 for(oldwin= oldwm->windows.first; oldwin; oldwin= oldwin->next) { 00242 00243 if(oldwin->winid == win->winid ) { 00244 win->ghostwin= oldwin->ghostwin; 00245 win->active= oldwin->active; 00246 if(win->active) 00247 wm->winactive= win; 00248 00249 if(!G.background) /* file loading in background mode still calls this */ 00250 GHOST_SetWindowUserData(win->ghostwin, win); /* pointer back */ 00251 00252 oldwin->ghostwin= NULL; 00253 00254 win->eventstate= oldwin->eventstate; 00255 oldwin->eventstate= NULL; 00256 00257 /* ensure proper screen rescaling */ 00258 win->sizex= oldwin->sizex; 00259 win->sizey= oldwin->sizey; 00260 win->posx= oldwin->posx; 00261 win->posy= oldwin->posy; 00262 } 00263 } 00264 } 00265 wm_close_and_free_all(C, oldwmlist); 00266 } 00267 } 00268 } 00269 00270 /* in case UserDef was read, we re-initialize all, and do versioning */ 00271 static void wm_init_userdef(bContext *C) 00272 { 00273 UI_init_userdef(); 00274 MEM_CacheLimiter_set_maximum(U.memcachelimit * 1024 * 1024); 00275 sound_init(CTX_data_main(C)); 00276 00277 /* needed so loading a file from the command line respects user-pref [#26156] */ 00278 if(U.flag & USER_FILENOUI) G.fileflags |= G_FILE_NO_UI; 00279 else G.fileflags &= ~G_FILE_NO_UI; 00280 00281 /* set the python auto-execute setting from user prefs */ 00282 /* enabled by default, unless explicitly enabled in the command line which overrides */ 00283 if((G.f & G_SCRIPT_OVERRIDE_PREF) == 0) { 00284 if ((U.flag & USER_SCRIPT_AUTOEXEC_DISABLE) == 0) G.f |= G_SCRIPT_AUTOEXEC; 00285 else G.f &= ~G_SCRIPT_AUTOEXEC; 00286 } 00287 if(U.tempdir[0]) BLI_where_is_temp(btempdir, FILE_MAX, 1); 00288 } 00289 00290 00291 00292 /* return codes */ 00293 #define BKE_READ_EXOTIC_FAIL_PATH -3 /* file format is not supported */ 00294 #define BKE_READ_EXOTIC_FAIL_FORMAT -2 /* file format is not supported */ 00295 #define BKE_READ_EXOTIC_FAIL_OPEN -1 /* Can't open the file */ 00296 #define BKE_READ_EXOTIC_OK_BLEND 0 /* .blend file */ 00297 #define BKE_READ_EXOTIC_OK_OTHER 1 /* other supported formats */ 00298 00299 /* intended to check for non-blender formats but for now it only reads blends */ 00300 static int wm_read_exotic(Scene *UNUSED(scene), const char *name) 00301 { 00302 int len; 00303 gzFile gzfile; 00304 char header[7]; 00305 int retval; 00306 00307 // make sure we're not trying to read a directory.... 00308 00309 len= strlen(name); 00310 if (ELEM(name[len-1], '/', '\\')) { 00311 retval= BKE_READ_EXOTIC_FAIL_PATH; 00312 } 00313 else { 00314 gzfile = gzopen(name,"rb"); 00315 00316 if (gzfile == NULL) { 00317 retval= BKE_READ_EXOTIC_FAIL_OPEN; 00318 } 00319 else { 00320 len= gzread(gzfile, header, sizeof(header)); 00321 gzclose(gzfile); 00322 if (len == sizeof(header) && strncmp(header, "BLENDER", 7) == 0) { 00323 retval= BKE_READ_EXOTIC_OK_BLEND; 00324 } 00325 else { 00326 //XXX waitcursor(1); 00327 /* 00328 if(is_foo_format(name)) { 00329 read_foo(name); 00330 retval= BKE_READ_EXOTIC_OK_OTHER; 00331 } 00332 else 00333 */ 00334 { 00335 retval= BKE_READ_EXOTIC_FAIL_FORMAT; 00336 } 00337 //XXX waitcursor(0); 00338 } 00339 } 00340 } 00341 00342 return retval; 00343 } 00344 00345 void WM_read_file(bContext *C, const char *filepath, ReportList *reports) 00346 { 00347 int retval; 00348 00349 /* so we can get the error message */ 00350 errno = 0; 00351 00352 WM_cursor_wait(1); 00353 00354 BLI_exec_cb(CTX_data_main(C), NULL, BLI_CB_EVT_LOAD_PRE); 00355 00356 /* first try to append data from exotic file formats... */ 00357 /* it throws error box when file doesnt exist and returns -1 */ 00358 /* note; it should set some error message somewhere... (ton) */ 00359 retval= wm_read_exotic(CTX_data_scene(C), filepath); 00360 00361 /* we didn't succeed, now try to read Blender file */ 00362 if (retval == BKE_READ_EXOTIC_OK_BLEND) { 00363 int G_f= G.f; 00364 ListBase wmbase; 00365 00366 /* put aside screens to match with persistant windows later */ 00367 /* also exit screens and editors */ 00368 wm_window_match_init(C, &wmbase); 00369 00370 retval= BKE_read_file(C, filepath, reports); 00371 G.save_over = 1; 00372 00373 /* this flag is initialized by the operator but overwritten on read. 00374 * need to re-enable it here else drivers + registered scripts wont work. */ 00375 if(G.f != G_f) { 00376 const int flags_keep= (G_SCRIPT_AUTOEXEC | G_SCRIPT_OVERRIDE_PREF); 00377 G.f= (G.f & ~flags_keep) | (G_f & flags_keep); 00378 } 00379 00380 /* match the read WM with current WM */ 00381 wm_window_match_do(C, &wmbase); 00382 WM_check(C); /* opens window(s), checks keymaps */ 00383 00384 // XXX mainwindow_set_filename_to_title(G.main->name); 00385 00386 if(retval == BKE_READ_FILE_OK_USERPREFS) wm_init_userdef(C); // in case a userdef is read from regular .blend 00387 00388 if (retval != BKE_READ_FILE_FAIL) { 00389 G.relbase_valid = 1; 00390 if(!G.background) /* assume automated tasks with background, dont write recent file list */ 00391 write_history(); 00392 } 00393 00394 00395 WM_event_add_notifier(C, NC_WM|ND_FILEREAD, NULL); 00396 // refresh_interface_font(); 00397 00398 CTX_wm_window_set(C, CTX_wm_manager(C)->windows.first); 00399 00400 ED_editors_init(C); 00401 DAG_on_visible_update(CTX_data_main(C), TRUE); 00402 00403 #ifdef WITH_PYTHON 00404 /* run any texts that were loaded in and flagged as modules */ 00405 BPY_driver_reset(); 00406 BPY_app_handlers_reset(); 00407 BPY_modules_load_user(C); 00408 #endif 00409 CTX_wm_window_set(C, NULL); /* exits queues */ 00410 00411 #if 0 /* gives popups on windows but not linux, bug in report API but disable for now to stop users getting annoyed */ 00412 /* TODO, make this show in header info window */ 00413 { 00414 Scene *sce; 00415 for(sce= G.main->scene.first; sce; sce= sce->id.next) { 00416 if(sce->r.engine[0] && BLI_findstring(&R_engines, sce->r.engine, offsetof(RenderEngineType, idname)) == NULL) { 00417 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); 00418 } 00419 } 00420 } 00421 #endif 00422 00423 // XXX undo_editmode_clear(); 00424 BKE_reset_undo(); 00425 BKE_write_undo(C, "original"); /* save current state */ 00426 00427 BLI_exec_cb(CTX_data_main(C), NULL, BLI_CB_EVT_LOAD_POST); 00428 } 00429 else if(retval == BKE_READ_EXOTIC_OK_OTHER) 00430 BKE_write_undo(C, "Import file"); 00431 else if(retval == BKE_READ_EXOTIC_FAIL_OPEN) { 00432 BKE_reportf(reports, RPT_ERROR, "Can't read file: \"%s\", %s.", filepath, errno ? strerror(errno) : "Unable to open the file"); 00433 } 00434 else if(retval == BKE_READ_EXOTIC_FAIL_FORMAT) { 00435 BKE_reportf(reports, RPT_ERROR, "File format is not supported in file: \"%s\".", filepath); 00436 } 00437 else if(retval == BKE_READ_EXOTIC_FAIL_PATH) { 00438 BKE_reportf(reports, RPT_ERROR, "File path invalid: \"%s\".", filepath); 00439 } 00440 else { 00441 BKE_reportf(reports, RPT_ERROR, "Unknown error loading: \"%s\".", filepath); 00442 BLI_assert(!"invalid 'retval'"); 00443 } 00444 00445 WM_cursor_wait(0); 00446 00447 } 00448 00449 00450 /* called on startup, (context entirely filled with NULLs) */ 00451 /* or called for 'New File' */ 00452 /* op can be NULL */ 00453 int WM_read_homefile(bContext *C, ReportList *reports, short from_memory) 00454 { 00455 ListBase wmbase; 00456 char tstr[FILE_MAXDIR+FILE_MAXFILE]; 00457 int success= 0; 00458 00459 free_ttfont(); /* still weird... what does it here? */ 00460 00461 G.relbase_valid = 0; 00462 if (!from_memory) { 00463 char *cfgdir = BLI_get_folder(BLENDER_USER_CONFIG, NULL); 00464 if (cfgdir) { 00465 BLI_make_file_string(G.main->name, tstr, cfgdir, BLENDER_STARTUP_FILE); 00466 } else { 00467 tstr[0] = '\0'; 00468 from_memory = 1; 00469 BKE_report(reports, RPT_INFO, "Config directory with "STRINGIFY(BLENDER_STARTUP_FILE)" file not found."); 00470 } 00471 } 00472 00473 /* prevent loading no UI */ 00474 G.fileflags &= ~G_FILE_NO_UI; 00475 00476 /* put aside screens to match with persistant windows later */ 00477 wm_window_match_init(C, &wmbase); 00478 00479 if (!from_memory && BLI_exists(tstr)) { 00480 success = (BKE_read_file(C, tstr, NULL) != BKE_READ_FILE_FAIL); 00481 00482 if(U.themes.first==NULL) { 00483 printf("\nError: No valid "STRINGIFY(BLENDER_STARTUP_FILE)", fall back to built-in default.\n\n"); 00484 success = 0; 00485 } 00486 } 00487 if(success==0) { 00488 success = BKE_read_file_from_memory(C, datatoc_startup_blend, datatoc_startup_blend_size, NULL); 00489 if (wmbase.first == NULL) wm_clear_default_size(C); 00490 00491 #ifdef WITH_PYTHON_SECURITY /* not default */ 00492 /* use alternative setting for security nuts 00493 * otherwise we'd need to patch the binary blob - startup.blend.c */ 00494 U.flag |= USER_SCRIPT_AUTOEXEC_DISABLE; 00495 #endif 00496 } 00497 00498 /* prevent buggy files that had G_FILE_RELATIVE_REMAP written out by mistake. Screws up autosaves otherwise 00499 * can remove this eventually, only in a 2.53 and older, now its not written */ 00500 G.fileflags &= ~G_FILE_RELATIVE_REMAP; 00501 00502 /* check userdef before open window, keymaps etc */ 00503 wm_init_userdef(C); 00504 00505 /* match the read WM with current WM */ 00506 wm_window_match_do(C, &wmbase); 00507 WM_check(C); /* opens window(s), checks keymaps */ 00508 00509 G.main->name[0]= '\0'; 00510 00511 /* When loading factory settings, the reset solid OpenGL lights need to be applied. */ 00512 if (!G.background) GPU_default_lights(); 00513 00514 /* XXX */ 00515 G.save_over = 0; // start with save preference untitled.blend 00516 G.fileflags &= ~G_FILE_AUTOPLAY; /* disable autoplay in startup.blend... */ 00517 // mainwindow_set_filename_to_title(""); // empty string re-initializes title to "Blender" 00518 00519 // refresh_interface_font(); 00520 00521 // undo_editmode_clear(); 00522 BKE_reset_undo(); 00523 BKE_write_undo(C, "original"); /* save current state */ 00524 00525 ED_editors_init(C); 00526 DAG_on_visible_update(CTX_data_main(C), TRUE); 00527 00528 #ifdef WITH_PYTHON 00529 if(CTX_py_init_get(C)) { 00530 /* sync addons, these may have changed from the defaults */ 00531 BPY_string_exec(C, "__import__('addon_utils').reset_all()"); 00532 00533 BPY_driver_reset(); 00534 BPY_app_handlers_reset(); 00535 BPY_modules_load_user(C); 00536 } 00537 #endif 00538 00539 WM_event_add_notifier(C, NC_WM|ND_FILEREAD, NULL); 00540 00541 /* in background mode the scene will stay NULL */ 00542 if(!G.background) { 00543 CTX_wm_window_set(C, NULL); /* exits queues */ 00544 } 00545 00546 return TRUE; 00547 } 00548 00549 int WM_read_homefile_exec(bContext *C, wmOperator *op) 00550 { 00551 int from_memory= strcmp(op->type->idname, "WM_OT_read_factory_settings") == 0; 00552 return WM_read_homefile(C, op->reports, from_memory) ? OPERATOR_FINISHED : OPERATOR_CANCELLED; 00553 } 00554 00555 void WM_read_history(void) 00556 { 00557 char name[FILE_MAX]; 00558 LinkNode *l, *lines; 00559 struct RecentFile *recent; 00560 char *line; 00561 int num; 00562 char *cfgdir = BLI_get_folder(BLENDER_USER_CONFIG, NULL); 00563 00564 if (!cfgdir) return; 00565 00566 BLI_make_file_string("/", name, cfgdir, BLENDER_HISTORY_FILE); 00567 00568 lines= BLI_read_file_as_lines(name); 00569 00570 G.recent_files.first = G.recent_files.last = NULL; 00571 00572 /* read list of recent opend files from recent-files.txt to memory */ 00573 for (l= lines, num= 0; l && (num<U.recent_files); l= l->next) { 00574 line = l->link; 00575 if (line[0] && BLI_exists(line)) { 00576 recent = (RecentFile*)MEM_mallocN(sizeof(RecentFile),"RecentFile"); 00577 BLI_addtail(&(G.recent_files), recent); 00578 recent->filepath = BLI_strdup(line); 00579 num++; 00580 } 00581 } 00582 00583 BLI_free_file_lines(lines); 00584 00585 } 00586 00587 static void write_history(void) 00588 { 00589 struct RecentFile *recent, *next_recent; 00590 char name[FILE_MAXDIR+FILE_MAXFILE]; 00591 char *user_config_dir; 00592 FILE *fp; 00593 int i; 00594 00595 /* will be NULL in background mode */ 00596 user_config_dir = BLI_get_folder_create(BLENDER_USER_CONFIG, NULL); 00597 if(!user_config_dir) 00598 return; 00599 00600 BLI_make_file_string("/", name, user_config_dir, BLENDER_HISTORY_FILE); 00601 00602 recent = G.recent_files.first; 00603 /* refresh recent-files.txt of recent opened files, when current file was changed */ 00604 if(!(recent) || (BLI_path_cmp(recent->filepath, G.main->name)!=0)) { 00605 fp= fopen(name, "w"); 00606 if (fp) { 00607 /* add current file to the beginning of list */ 00608 recent = (RecentFile*)MEM_mallocN(sizeof(RecentFile),"RecentFile"); 00609 recent->filepath = BLI_strdup(G.main->name); 00610 BLI_addhead(&(G.recent_files), recent); 00611 /* write current file to recent-files.txt */ 00612 fprintf(fp, "%s\n", recent->filepath); 00613 recent = recent->next; 00614 i=1; 00615 /* write rest of recent opened files to recent-files.txt */ 00616 while((i<U.recent_files) && (recent)){ 00617 /* this prevents to have duplicities in list */ 00618 if (BLI_path_cmp(recent->filepath, G.main->name)!=0) { 00619 fprintf(fp, "%s\n", recent->filepath); 00620 recent = recent->next; 00621 } 00622 else { 00623 next_recent = recent->next; 00624 MEM_freeN(recent->filepath); 00625 BLI_freelinkN(&(G.recent_files), recent); 00626 recent = next_recent; 00627 } 00628 i++; 00629 } 00630 fclose(fp); 00631 } 00632 00633 /* also update most recent files on System */ 00634 GHOST_addToSystemRecentFiles(G.main->name); 00635 } 00636 } 00637 00638 static ImBuf *blend_file_thumb(Scene *scene, int **thumb_pt) 00639 { 00640 /* will be scaled down, but gives some nice oversampling */ 00641 ImBuf *ibuf; 00642 int *thumb; 00643 char err_out[256]= "unknown"; 00644 00645 *thumb_pt= NULL; 00646 00647 /* scene can be NULL if running a script at startup and calling the save operator */ 00648 if(G.background || scene==NULL || scene->camera==NULL) 00649 return NULL; 00650 00651 /* gets scaled to BLEN_THUMB_SIZE */ 00652 ibuf= ED_view3d_draw_offscreen_imbuf_simple(scene, scene->camera, BLEN_THUMB_SIZE * 2, BLEN_THUMB_SIZE * 2, IB_rect, OB_SOLID, err_out); 00653 00654 if(ibuf) { 00655 float aspect= (scene->r.xsch*scene->r.xasp) / (scene->r.ysch*scene->r.yasp); 00656 00657 /* dirty oversampling */ 00658 IMB_scaleImBuf(ibuf, BLEN_THUMB_SIZE, BLEN_THUMB_SIZE); 00659 00660 /* add pretty overlay */ 00661 IMB_overlayblend_thumb(ibuf->rect, ibuf->x, ibuf->y, aspect); 00662 00663 /* first write into thumb buffer */ 00664 thumb= MEM_mallocN(((2 + (BLEN_THUMB_SIZE * BLEN_THUMB_SIZE))) * sizeof(int), "write_file thumb"); 00665 00666 thumb[0] = BLEN_THUMB_SIZE; 00667 thumb[1] = BLEN_THUMB_SIZE; 00668 00669 memcpy(thumb + 2, ibuf->rect, BLEN_THUMB_SIZE * BLEN_THUMB_SIZE * sizeof(int)); 00670 } 00671 else { 00672 /* '*thumb_pt' needs to stay NULL to prevent a bad thumbnail from being handled */ 00673 fprintf(stderr, "blend_file_thumb failed to create thumbnail: %s\n", err_out); 00674 thumb= NULL; 00675 } 00676 00677 /* must be freed by caller */ 00678 *thumb_pt= thumb; 00679 00680 return ibuf; 00681 } 00682 00683 /* easy access from gdb */ 00684 int write_crash_blend(void) 00685 { 00686 char path[FILE_MAX]; 00687 int fileflags = G.fileflags & ~(G_FILE_HISTORY); /* don't do file history on crash file */ 00688 00689 BLI_strncpy(path, G.main->name, sizeof(path)); 00690 BLI_replace_extension(path, sizeof(path), "_crash.blend"); 00691 if(BLO_write_file(G.main, path, fileflags, NULL, NULL)) { 00692 printf("written: %s\n", path); 00693 return 1; 00694 } 00695 else { 00696 printf("failed: %s\n", path); 00697 return 0; 00698 } 00699 } 00700 00701 int WM_write_file(bContext *C, const char *target, int fileflags, ReportList *reports, int copy) 00702 { 00703 Library *li; 00704 int len; 00705 char filepath[FILE_MAX]; 00706 00707 int *thumb= NULL; 00708 ImBuf *ibuf_thumb= NULL; 00709 00710 len = strlen(target); 00711 00712 if (len == 0) { 00713 BKE_report(reports, RPT_ERROR, "Path is empty, cannot save"); 00714 return -1; 00715 } 00716 00717 if (len >= FILE_MAX) { 00718 BKE_report(reports, RPT_ERROR, "Path too long, cannot save"); 00719 return -1; 00720 } 00721 00722 BLI_strncpy(filepath, target, FILE_MAX); 00723 BLI_replace_extension(filepath, FILE_MAX, ".blend"); 00724 /* dont use 'target' anymore */ 00725 00726 /* send the OnSave event */ 00727 for (li= G.main->library.first; li; li= li->id.next) { 00728 if (BLI_path_cmp(li->filepath, filepath) == 0) { 00729 BKE_reportf(reports, RPT_ERROR, "Can't overwrite used library '%.200s'", filepath); 00730 return -1; 00731 } 00732 } 00733 00734 /* blend file thumbnail */ 00735 /* save before exit_editmode, otherwise derivedmeshes for shared data corrupt #27765) */ 00736 if(U.flag & USER_SAVE_PREVIEWS) { 00737 ibuf_thumb= blend_file_thumb(CTX_data_scene(C), &thumb); 00738 } 00739 00740 BLI_exec_cb(G.main, NULL, BLI_CB_EVT_SAVE_PRE); 00741 00742 /* operator now handles overwrite checks */ 00743 00744 if (G.fileflags & G_AUTOPACK) { 00745 packAll(G.main, reports); 00746 } 00747 00748 ED_object_exit_editmode(C, EM_DO_UNDO); 00749 ED_sculpt_force_update(C); 00750 00751 /* don't forget not to return without! */ 00752 WM_cursor_wait(1); 00753 00754 fileflags |= G_FILE_HISTORY; /* write file history */ 00755 00756 if (BLO_write_file(CTX_data_main(C), filepath, fileflags, reports, thumb)) { 00757 if(!copy) { 00758 G.relbase_valid = 1; 00759 BLI_strncpy(G.main->name, filepath, sizeof(G.main->name)); /* is guaranteed current file */ 00760 00761 G.save_over = 1; /* disable untitled.blend convention */ 00762 } 00763 00764 if(fileflags & G_FILE_COMPRESS) G.fileflags |= G_FILE_COMPRESS; 00765 else G.fileflags &= ~G_FILE_COMPRESS; 00766 00767 if(fileflags & G_FILE_AUTOPLAY) G.fileflags |= G_FILE_AUTOPLAY; 00768 else G.fileflags &= ~G_FILE_AUTOPLAY; 00769 00770 /* prevent background mode scripts from clobbering history */ 00771 if(!G.background) { 00772 write_history(); 00773 } 00774 00775 BLI_exec_cb(G.main, NULL, BLI_CB_EVT_SAVE_POST); 00776 00777 /* run this function after because the file cant be written before the blend is */ 00778 if (ibuf_thumb) { 00779 ibuf_thumb= IMB_thumb_create(filepath, THB_NORMAL, THB_SOURCE_BLEND, ibuf_thumb); 00780 IMB_freeImBuf(ibuf_thumb); 00781 } 00782 00783 if(thumb) MEM_freeN(thumb); 00784 } 00785 else { 00786 if(ibuf_thumb) IMB_freeImBuf(ibuf_thumb); 00787 if(thumb) MEM_freeN(thumb); 00788 00789 WM_cursor_wait(0); 00790 return -1; 00791 } 00792 00793 WM_cursor_wait(0); 00794 00795 return 0; 00796 } 00797 00798 /* operator entry */ 00799 int WM_write_homefile(bContext *C, wmOperator *op) 00800 { 00801 wmWindowManager *wm= CTX_wm_manager(C); 00802 wmWindow *win= CTX_wm_window(C); 00803 char filepath[FILE_MAXDIR+FILE_MAXFILE]; 00804 int fileflags; 00805 00806 /* check current window and close it if temp */ 00807 if(win->screen->temp) 00808 wm_window_close(C, wm, win); 00809 00810 /* update keymaps in user preferences */ 00811 WM_keyconfig_update(wm); 00812 00813 BLI_make_file_string("/", filepath, BLI_get_folder_create(BLENDER_USER_CONFIG, NULL), BLENDER_STARTUP_FILE); 00814 printf("trying to save homefile at %s ", filepath); 00815 00816 /* force save as regular blend file */ 00817 fileflags = G.fileflags & ~(G_FILE_COMPRESS | G_FILE_AUTOPLAY | G_FILE_LOCK | G_FILE_SIGN | G_FILE_HISTORY); 00818 00819 if(BLO_write_file(CTX_data_main(C), filepath, fileflags, op->reports, NULL) == 0) { 00820 printf("fail\n"); 00821 return OPERATOR_CANCELLED; 00822 } 00823 00824 printf("ok\n"); 00825 00826 G.save_over= 0; 00827 00828 return OPERATOR_FINISHED; 00829 } 00830 00831 /************************ autosave ****************************/ 00832 00833 void wm_autosave_location(char *filepath) 00834 { 00835 char pidstr[32]; 00836 #ifdef WIN32 00837 char *savedir; 00838 #endif 00839 00840 BLI_snprintf(pidstr, sizeof(pidstr), "%d.blend", abs(getpid())); 00841 00842 #ifdef WIN32 00843 /* XXX Need to investigate how to handle default location of '/tmp/' 00844 * This is a relative directory on Windows, and it may be 00845 * found. Example: 00846 * Blender installed on D:\ drive, D:\ drive has D:\tmp\ 00847 * Now, BLI_exists() will find '/tmp/' exists, but 00848 * BLI_make_file_string will create string that has it most likely on C:\ 00849 * through get_default_root(). 00850 * If there is no C:\tmp autosave fails. */ 00851 if (!BLI_exists(U.tempdir)) { 00852 savedir = BLI_get_folder_create(BLENDER_USER_AUTOSAVE, NULL); 00853 BLI_make_file_string("/", filepath, savedir, pidstr); 00854 return; 00855 } 00856 #endif 00857 00858 BLI_make_file_string("/", filepath, U.tempdir, pidstr); 00859 } 00860 00861 void WM_autosave_init(wmWindowManager *wm) 00862 { 00863 wm_autosave_timer_ended(wm); 00864 00865 if(U.flag & USER_AUTOSAVE) 00866 wm->autosavetimer= WM_event_add_timer(wm, NULL, TIMERAUTOSAVE, U.savetime*60.0); 00867 } 00868 00869 void wm_autosave_timer(const bContext *C, wmWindowManager *wm, wmTimer *UNUSED(wt)) 00870 { 00871 wmWindow *win; 00872 wmEventHandler *handler; 00873 char filepath[FILE_MAX]; 00874 int fileflags; 00875 00876 WM_event_remove_timer(wm, NULL, wm->autosavetimer); 00877 00878 /* if a modal operator is running, don't autosave, but try again in 10 seconds */ 00879 for(win=wm->windows.first; win; win=win->next) { 00880 for(handler=win->modalhandlers.first; handler; handler=handler->next) { 00881 if(handler->op) { 00882 wm->autosavetimer= WM_event_add_timer(wm, NULL, TIMERAUTOSAVE, 10.0); 00883 return; 00884 } 00885 } 00886 } 00887 00888 wm_autosave_location(filepath); 00889 00890 /* force save as regular blend file */ 00891 fileflags = G.fileflags & ~(G_FILE_COMPRESS|G_FILE_AUTOPLAY |G_FILE_LOCK|G_FILE_SIGN|G_FILE_HISTORY); 00892 00893 /* no error reporting to console */ 00894 BLO_write_file(CTX_data_main(C), filepath, fileflags, NULL, NULL); 00895 00896 /* do timer after file write, just in case file write takes a long time */ 00897 wm->autosavetimer= WM_event_add_timer(wm, NULL, TIMERAUTOSAVE, U.savetime*60.0); 00898 } 00899 00900 void wm_autosave_timer_ended(wmWindowManager *wm) 00901 { 00902 if(wm->autosavetimer) { 00903 WM_event_remove_timer(wm, NULL, wm->autosavetimer); 00904 wm->autosavetimer= NULL; 00905 } 00906 } 00907 00908 void wm_autosave_delete(void) 00909 { 00910 char filename[FILE_MAX]; 00911 00912 wm_autosave_location(filename); 00913 00914 if(BLI_exists(filename)) { 00915 char str[FILE_MAXDIR+FILE_MAXFILE]; 00916 BLI_make_file_string("/", str, U.tempdir, "quit.blend"); 00917 00918 /* if global undo; remove tempsave, otherwise rename */ 00919 if(U.uiflag & USER_GLOBALUNDO) BLI_delete(filename, 0, 0); 00920 else BLI_rename(filename, str); 00921 } 00922 } 00923 00924 void wm_autosave_read(bContext *C, ReportList *reports) 00925 { 00926 char filename[FILE_MAX]; 00927 00928 wm_autosave_location(filename); 00929 WM_read_file(C, filename, reports); 00930 } 00931