2.5
[blender.git] / source / blender / windowmanager / intern / wm_files.c
1 /**
2  * $Id: wm_files.c
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
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. 
10  *
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.
15  *
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation 2007
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28         /* placed up here because of crappy
29          * winsock stuff.
30          */
31 #include <stdio.h>
32 #include <string.h>
33
34 #ifdef WIN32
35 #include <windows.h> /* need to include windows.h so _WIN32_IE is defined  */
36 #ifndef _WIN32_IE
37 #define _WIN32_IE 0x0400 /* minimal requirements for SHGetSpecialFolderPath on MINGW MSVC has this defined already */
38 #endif
39 #include <shlobj.h> /* for SHGetSpecialFolderPath, has to be done before BLI_winstuff because 'near' is disabled through BLI_windstuff */
40 #include "BLI_winstuff.h"
41 #include <process.h> /* getpid */
42 #else
43 #include <unistd.h> /* getpid */
44 #endif
45
46 #include "MEM_guardedalloc.h"
47 #include "MEM_CacheLimiterC-Api.h"
48
49 #include "BLI_blenlib.h"
50 #include "BLI_linklist.h"
51
52 #include "DNA_anim_types.h"
53 #include "DNA_ipo_types.h" // XXX old animation system
54 #include "DNA_object_types.h"
55 #include "DNA_space_types.h"
56 #include "DNA_userdef_types.h"
57 #include "DNA_scene_types.h"
58 #include "DNA_screen_types.h"
59 #include "DNA_windowmanager_types.h"
60
61 #include "BKE_blender.h"
62 #include "BKE_context.h"
63 #include "BKE_DerivedMesh.h"
64 #include "BKE_exotic.h"
65 #include "BKE_font.h"
66 #include "BKE_global.h"
67 #include "BKE_library.h"
68 #include "BKE_main.h"
69 #include "BKE_packedFile.h"
70 #include "BKE_report.h"
71 #include "BKE_texture.h"
72 #include "BKE_utildefines.h"
73
74 #include "BLO_readfile.h"
75 #include "BLO_writefile.h"
76
77 #include "RNA_access.h"
78 #include "RNA_define.h"
79
80 #include "ED_datafiles.h"
81 #include "ED_screen.h"
82 #include "ED_util.h"
83
84 #include "GHOST_C-api.h"
85
86 #include "UI_interface.h"
87 #include "BLF_api.h"
88
89 #include "GPU_draw.h"
90
91 // XXX #include "BPY_extern.h"
92
93 #include "WM_api.h"
94 #include "WM_types.h"
95 #include "wm.h"
96
97 /***/
98
99 /* define for setting colors in theme below */
100 #define SETCOL(col, r, g, b, a)  {col[0]=r; col[1]=g; col[2]= b; col[3]= a;}
101
102 /* patching UserDef struct and Themes */
103 static void init_userdef_themes(void)
104 {
105 //      countall();
106         
107         /* the UserDef struct is not corrected with do_versions() .... ugh! */
108         if(U.wheellinescroll == 0) U.wheellinescroll = 3;
109         if(U.menuthreshold1==0) {
110                 U.menuthreshold1= 5;
111                 U.menuthreshold2= 2;
112         }
113         if(U.tb_leftmouse==0) {
114                 U.tb_leftmouse= 5;
115                 U.tb_rightmouse= 5;
116         }
117         if(U.mixbufsize==0) U.mixbufsize= 2048;
118         if (BLI_streq(U.tempdir, "/")) {
119                 char *tmp= getenv("TEMP");
120                 
121                 strcpy(U.tempdir, tmp?tmp:"/tmp/");
122         }
123         if (U.savetime <= 0) {
124                 U.savetime = 1;
125 // XXX          error(".B.blend is buggy, please consider removing it.\n");
126         }
127         /* transform widget settings */
128         if(U.tw_hotspot==0) {
129                 U.tw_hotspot= 14;
130                 U.tw_size= 20;                  // percentage of window size
131                 U.tw_handlesize= 16;    // percentage of widget radius
132         }
133         if(U.pad_rot_angle==0)
134                 U.pad_rot_angle= 15;
135         
136         if(U.flag & USER_CUSTOM_RANGE) 
137                 vDM_ColorBand_store(&U.coba_weight); /* signal for derivedmesh to use colorband */
138         
139         if (G.main->versionfile <= 191) {
140                 strcpy(U.plugtexdir, U.textudir);
141                 strcpy(U.sounddir, "/");
142         }
143         
144         /* patch to set Dupli Armature */
145         if (G.main->versionfile < 220) {
146                 U.dupflag |= USER_DUP_ARM;
147         }
148         
149         /* userdef new option */
150         if (G.main->versionfile <= 222) {
151                 U.vrmlflag= USER_VRML_LAYERS;
152         }
153         
154         /* added seam, normal color, undo */
155         if (G.main->versionfile <= 234) {
156                 bTheme *btheme;
157                 
158                 U.uiflag |= USER_GLOBALUNDO;
159                 if (U.undosteps==0) U.undosteps=32;
160                 
161                 for(btheme= U.themes.first; btheme; btheme= btheme->next) {
162                         /* check for alpha==0 is safe, then color was never set */
163                         if(btheme->tv3d.edge_seam[3]==0) {
164                                 SETCOL(btheme->tv3d.edge_seam, 230, 150, 50, 255);
165                         }
166                         if(btheme->tv3d.normal[3]==0) {
167                                 SETCOL(btheme->tv3d.normal, 0x22, 0xDD, 0xDD, 255);
168                         }
169                         if(btheme->tv3d.face_dot[3]==0) {
170                                 SETCOL(btheme->tv3d.face_dot, 255, 138, 48, 255);
171                                 btheme->tv3d.facedot_size= 4;
172                         }
173                 }
174         }
175         if (G.main->versionfile <= 235) {
176                 /* illegal combo... */
177                 if (U.flag & USER_LMOUSESELECT) 
178                         U.flag &= ~USER_TWOBUTTONMOUSE;
179         }
180         if (G.main->versionfile <= 236) {
181                 bTheme *btheme;
182                 /* new space type */
183                 for(btheme= U.themes.first; btheme; btheme= btheme->next) {
184                         /* check for alpha==0 is safe, then color was never set */
185                         if(btheme->ttime.back[3]==0) {
186                                 btheme->ttime = btheme->tsnd;   // copy from sound
187                         }
188                         if(btheme->text.syntaxn[3]==0) {
189                                 SETCOL(btheme->text.syntaxn,    0, 0, 200, 255);        /* Numbers  Blue*/
190                                 SETCOL(btheme->text.syntaxl,    100, 0, 0, 255);        /* Strings  red */
191                                 SETCOL(btheme->text.syntaxc,    0, 100, 50, 255);       /* Comments greenish */
192                                 SETCOL(btheme->text.syntaxv,    95, 95, 0, 255);        /* Special */
193                                 SETCOL(btheme->text.syntaxb,    128, 0, 80, 255);       /* Builtin, red-purple */
194                         }
195                 }
196         }
197         if (G.main->versionfile <= 237) {
198                 bTheme *btheme;
199                 /* bone colors */
200                 for(btheme= U.themes.first; btheme; btheme= btheme->next) {
201                         /* check for alpha==0 is safe, then color was never set */
202                         if(btheme->tv3d.bone_solid[3]==0) {
203                                 SETCOL(btheme->tv3d.bone_solid, 200, 200, 200, 255);
204                                 SETCOL(btheme->tv3d.bone_pose, 80, 200, 255, 80);
205                         }
206                 }
207         }
208         if (G.main->versionfile <= 238) {
209                 bTheme *btheme;
210                 /* bone colors */
211                 for(btheme= U.themes.first; btheme; btheme= btheme->next) {
212                         /* check for alpha==0 is safe, then color was never set */
213                         if(btheme->tnla.strip[3]==0) {
214                                 SETCOL(btheme->tnla.strip_select,       0xff, 0xff, 0xaa, 255);
215                                 SETCOL(btheme->tnla.strip, 0xe4, 0x9c, 0xc6, 255);
216                         }
217                 }
218         }
219         if (G.main->versionfile <= 239) {
220                 bTheme *btheme;
221                 
222                 for(btheme= U.themes.first; btheme; btheme= btheme->next) {
223                         /* Lamp theme, check for alpha==0 is safe, then color was never set */
224                         if(btheme->tv3d.lamp[3]==0) {
225                                 SETCOL(btheme->tv3d.lamp,       0, 0, 0, 40);
226 /* TEMPORAL, remove me! (ton) */                                
227                                 U.uiflag |= USER_PLAINMENUS;
228                         }
229                         
230                         /* check for text field selection highlight, set it to text editor highlight by default */
231                         if(btheme->tui.textfield_hi[3]==0) {
232                                 SETCOL(btheme->tui.textfield_hi,        
233                                         btheme->text.shade2[0], 
234                                         btheme->text.shade2[1], 
235                                         btheme->text.shade2[2],
236                                         255);
237                         }
238                 }
239                 if(U.obcenter_dia==0) U.obcenter_dia= 6;
240         }
241         if (G.main->versionfile <= 241) {
242                 bTheme *btheme;
243                 for(btheme= U.themes.first; btheme; btheme= btheme->next) {
244                         /* Node editor theme, check for alpha==0 is safe, then color was never set */
245                         if(btheme->tnode.syntaxn[3]==0) {
246                                 /* re-uses syntax color storage */
247                                 btheme->tnode= btheme->tv3d;
248                                 SETCOL(btheme->tnode.edge_select, 255, 255, 255, 255);
249                                 SETCOL(btheme->tnode.syntaxl, 150, 150, 150, 255);      /* TH_NODE, backdrop */
250                                 SETCOL(btheme->tnode.syntaxn, 129, 131, 144, 255);      /* in/output */
251                                 SETCOL(btheme->tnode.syntaxb, 127,127,127, 255);        /* operator */
252                                 SETCOL(btheme->tnode.syntaxv, 142, 138, 145, 255);      /* generator */
253                                 SETCOL(btheme->tnode.syntaxc, 120, 145, 120, 255);      /* group */
254                         }
255                         /* Group theme colors */
256                         if(btheme->tv3d.group[3]==0) {
257                                 SETCOL(btheme->tv3d.group, 0x10, 0x40, 0x10, 255);
258                                 SETCOL(btheme->tv3d.group_active, 0x66, 0xFF, 0x66, 255);
259                         }
260                         /* Sequence editor theme*/
261                         if(btheme->tseq.movie[3]==0) {
262                                 SETCOL(btheme->tseq.movie,      81, 105, 135, 255);
263                                 SETCOL(btheme->tseq.image,      109, 88, 129, 255);
264                                 SETCOL(btheme->tseq.scene,      78, 152, 62, 255);
265                                 SETCOL(btheme->tseq.audio,      46, 143, 143, 255);
266                                 SETCOL(btheme->tseq.effect,     169, 84, 124, 255);
267                                 SETCOL(btheme->tseq.plugin,     126, 126, 80, 255);
268                                 SETCOL(btheme->tseq.transition, 162, 95, 111, 255);
269                                 SETCOL(btheme->tseq.meta,       109, 145, 131, 255);
270                         }
271                         if(!(btheme->tui.iconfile)) {
272                                 BLI_strncpy(btheme->tui.iconfile, "", sizeof(btheme->tui.iconfile));
273                         }
274                 }
275                 
276                 /* set defaults for 3D View rotating axis indicator */ 
277                 /* since size can't be set to 0, this indicates it's not saved in .B.blend */
278                 if (U.rvisize == 0) {
279                         U.rvisize = 15;
280                         U.rvibright = 8;
281                         U.uiflag |= USER_SHOW_ROTVIEWICON;
282                 }
283                 
284         }
285         if (G.main->versionfile <= 242) {
286                 bTheme *btheme;
287                 
288                 for(btheme= U.themes.first; btheme; btheme= btheme->next) {
289                         /* long keyframe color */
290                         /* check for alpha==0 is safe, then color was never set */
291                         if(btheme->tact.strip[3]==0) {
292                                 SETCOL(btheme->tv3d.edge_sharp, 255, 32, 32, 255);
293                                 SETCOL(btheme->tact.strip_select,       0xff, 0xff, 0xaa, 204);
294                                 SETCOL(btheme->tact.strip, 0xe4, 0x9c, 0xc6, 204);
295                         }
296                         
297                         /* IPO-Editor - Vertex Size*/
298                         if(btheme->tipo.vertex_size == 0) {
299                                 btheme->tipo.vertex_size= 3;
300                         }
301                 }
302         }
303         if (G.main->versionfile <= 243) {
304                 /* set default number of recently-used files (if not set) */
305                 if (U.recent_files == 0) U.recent_files = 10;
306         }
307         if (G.main->versionfile < 245 || (G.main->versionfile == 245 && G.main->subversionfile < 3)) {
308                 bTheme *btheme;
309                 for(btheme= U.themes.first; btheme; btheme= btheme->next) {
310                         SETCOL(btheme->tv3d.editmesh_active, 255, 255, 255, 128);
311                 }
312                 if(U.coba_weight.tot==0)
313                         init_colorband(&U.coba_weight, 1);
314         }
315         if ((G.main->versionfile < 245) || (G.main->versionfile == 245 && G.main->subversionfile < 11)) {
316                 bTheme *btheme;
317                 for (btheme= U.themes.first; btheme; btheme= btheme->next) {
318                         /* these should all use the same colour */
319                         SETCOL(btheme->tv3d.cframe, 0x60, 0xc0, 0x40, 255);
320                         SETCOL(btheme->tipo.cframe, 0x60, 0xc0, 0x40, 255);
321                         SETCOL(btheme->tact.cframe, 0x60, 0xc0, 0x40, 255);
322                         SETCOL(btheme->tnla.cframe, 0x60, 0xc0, 0x40, 255);
323                         SETCOL(btheme->tseq.cframe, 0x60, 0xc0, 0x40, 255);
324                         SETCOL(btheme->tsnd.cframe, 0x60, 0xc0, 0x40, 255);
325                         SETCOL(btheme->ttime.cframe, 0x60, 0xc0, 0x40, 255);
326                 }
327         }
328         if ((G.main->versionfile < 248) || (G.main->versionfile == 248 && G.main->subversionfile < 3)) {
329                 bTheme *btheme;
330                 
331                 /* adjust themes */
332                 for (btheme= U.themes.first; btheme; btheme= btheme->next) {
333                         /* DopeSheet - (Object) Channel color */
334                         SETCOL(btheme->tact.ds_channel, 0x36, 0x13, 0xca, 255);
335                         SETCOL(btheme->tact.ds_subchannel, 0x60, 0x43, 0xd2, 255);
336                         
337                         /* Graph Editor - (Object) Channel color */
338                         SETCOL(btheme->tipo.ds_channel, 0x36, 0x13, 0xca, 255);
339                         SETCOL(btheme->tipo.ds_subchannel, 0x60, 0x43, 0xd2, 255);
340                 }
341                 
342                 /* adjust grease-pencil distances */
343                 U.gp_manhattendist= 1;
344                 U.gp_euclideandist= 2;
345                 
346                 /* adjust default interpolation for new IPO-curves */
347                 U.ipo_new= BEZT_IPO_BEZ;
348         }
349         if (G.main->versionfile < 250) {
350                 bTheme *btheme;
351
352                 /* this was not properly initialized in 2.45 */
353                 for(btheme= U.themes.first; btheme; btheme= btheme->next) {
354                         if(btheme->tima.face_dot[3]==0) {
355                                 SETCOL(btheme->tima.editmesh_active, 255, 255, 255, 128);
356                                 SETCOL(btheme->tima.face_dot, 255, 133, 0, 255);
357                                 btheme->tima.facedot_size= 2;
358                         }
359                 }
360         }
361         
362         /* GL Texture Garbage Collection (variable abused above!) */
363         if (U.textimeout == 0) {
364                 U.texcollectrate = 60;
365                 U.textimeout = 120;
366         }
367         if (U.memcachelimit <= 0) {
368                 U.memcachelimit = 32;
369         }
370         if (U.frameserverport == 0) {
371                 U.frameserverport = 8080;
372         }
373
374         MEM_CacheLimiter_set_maximum(U.memcachelimit * 1024 * 1024);
375         
376         // sets themes, fonts, .. from userdef
377         UI_init_userdef();
378         
379         /* funny name, but it is GE stuff, moves userdef stuff to engine */
380 // XXX  space_set_commmandline_options();
381         /* this timer uses U */
382 // XXX  reset_autosave();
383
384 }
385
386 /* To be able to read files without windows closing, opening, moving 
387    we try to prepare for worst case:
388    - active window gets active screen from file 
389    - restoring the screens from non-active windows 
390    Best case is all screens match, in that case they get assigned to proper window  
391 */
392 static void wm_window_match_init(bContext *C, ListBase *wmlist)
393 {
394         wmWindowManager *wm= G.main->wm.first;
395         wmWindow *win;
396         
397         *wmlist= G.main->wm;
398         G.main->wm.first= G.main->wm.last= NULL;
399         
400         /* first wrap up running stuff */
401         /* code copied from wm_init_exit.c */
402         for(wm= wmlist->first; wm; wm= wm->id.next) {
403                 
404                 WM_jobs_stop_all(wm);
405                 
406                 for(win= wm->windows.first; win; win= win->next) {
407                 
408                         CTX_wm_window_set(C, win);      /* needed by operator close callbacks */
409                         ED_screen_exit(C, win, win->screen);
410                 }
411         }
412         
413         ED_editors_exit(C);
414         
415 return; 
416         if(wm==NULL) return;
417         if(G.fileflags & G_FILE_NO_UI) return;
418         
419         /* we take apart the used screens from non-active window */
420         for(win= wm->windows.first; win; win= win->next) {
421                 BLI_strncpy(win->screenname, win->screen->id.name, MAX_ID_NAME);
422                 if(win!=wm->winactive) {
423                         BLI_remlink(&G.main->screen, win->screen);
424                         //BLI_addtail(screenbase, win->screen);
425                 }
426         }
427 }
428
429 /* match old WM with new, 4 cases:
430   1- no current wm, no read wm: make new default
431   2- no current wm, but read wm: that's OK, do nothing
432   3- current wm, but not in file: try match screen names
433   4- current wm, and wm in file: try match ghostwin 
434 */
435
436 static void wm_window_match_do(bContext *C, ListBase *oldwmlist)
437 {
438         wmWindowManager *oldwm, *wm;
439         wmWindow *oldwin, *win;
440         
441         /* cases 1 and 2 */
442         if(oldwmlist->first==NULL) {
443                 if(G.main->wm.first); /* nothing todo */
444                 else
445                         wm_add_default(C);
446         }
447         else {
448                 /* cases 3 and 4 */
449                 
450                 /* we've read file without wm..., keep current one entirely alive */
451                 if(G.main->wm.first==NULL) {
452                         bScreen *screen= CTX_wm_screen(C);
453                         
454                         /* match oldwm to new dbase, only old files */
455                         
456                         for(wm= oldwmlist->first; wm; wm= wm->id.next) {
457                                 
458                                 for(win= wm->windows.first; win; win= win->next) {
459                                         /* all windows get active screen from file */
460                                         if(screen->winid==0)
461                                                 win->screen= screen;
462                                         else 
463                                                 win->screen= ED_screen_duplicate(win, screen);
464                                         
465                                         BLI_strncpy(win->screenname, win->screen->id.name+2, 21);
466                                         win->screen->winid= win->winid;
467                                 }
468                         }
469                         
470                         G.main->wm= *oldwmlist;
471                         
472                         /* screens were read from file! */
473                         ED_screens_initialize(G.main->wm.first);
474                 }
475                 else {
476                         /* what if old was 3, and loaded 1? */
477                         /* this code could move to setup_appdata */
478                         oldwm= oldwmlist->first;
479                         wm= G.main->wm.first;
480
481                         /* ensure making new keymaps and set space types */
482                         wm->initialized= 0;
483                         
484                         /* only first wm in list has ghostwins */
485                         for(win= wm->windows.first; win; win= win->next) {
486                                 for(oldwin= oldwm->windows.first; oldwin; oldwin= oldwin->next) {
487                                         
488                                         if(oldwin->winid == win->winid ) {
489                                                 win->ghostwin= oldwin->ghostwin;
490                                                 GHOST_SetWindowUserData(win->ghostwin, win);    /* pointer back */
491                                                 oldwin->ghostwin= NULL;
492                                                 
493                                                 win->eventstate= oldwin->eventstate;
494                                                 oldwin->eventstate= NULL;
495                                         }
496                                 }
497                         }
498                         wm_close_and_free_all(C, oldwmlist);
499                 }
500         }
501 }
502
503 void WM_read_file(bContext *C, char *name, ReportList *reports)
504 {
505         int retval;
506
507         /* first try to append data from exotic file formats... */
508         /* it throws error box when file doesnt exist and returns -1 */
509         /* note; it should set some error message somewhere... (ton) */
510         retval= BKE_read_exotic(CTX_data_scene(C), name);
511         
512         /* we didn't succeed, now try to read Blender file */
513         if (retval== 0) {
514                 ListBase wmbase;
515
516                 /* put aside screens to match with persistant windows later */
517                 /* also exit screens and editors */
518                 wm_window_match_init(C, &wmbase); 
519                 
520                 retval= BKE_read_file(C, name, NULL, reports);
521
522                 /* match the read WM with current WM */
523                 wm_window_match_do(C, &wmbase); 
524                 wm_check(C); /* opens window(s), checks keymaps */
525                 
526 // XXX          mainwindow_set_filename_to_title(G.main->name);
527 // XXX          sound_initialize_sounds();
528
529                 if(retval==2) init_userdef_themes();    // in case a userdef is read from regular .blend
530                 
531                 if (retval!=0) G.relbase_valid = 1;
532
533 // XXX          undo_editmode_clear();
534                 BKE_reset_undo();
535                 BKE_write_undo(C, "original");  /* save current state */
536
537                 WM_event_add_notifier(C, NC_WM|ND_FILEREAD, NULL);
538 //              refresh_interface_font();
539                                            
540                 CTX_wm_window_set(C, NULL); /* exits queues */
541         }
542         else if(retval==1)
543                 BKE_write_undo(C, "Import file");
544         else if(retval == -1) {
545                 if(reports && reports->list.first == NULL)
546                         BKE_report(reports, RPT_ERROR, "Cannot read file.");
547         }
548 }
549
550
551 /* called on startup,  (context entirely filled with NULLs) */
552 /* or called for 'Erase All' */
553 int WM_read_homefile(bContext *C, wmOperator *op)
554 {
555         ListBase wmbase;
556         char tstr[FILE_MAXDIR+FILE_MAXFILE], scestr[FILE_MAXDIR];
557         char *home= BLI_gethome();
558         int from_memory= op?RNA_boolean_get(op->ptr, "factory"):0;
559         int success;
560                 
561         BLI_clean(home);
562         
563         free_ttfont(); /* still weird... what does it here? */
564                 
565         G.relbase_valid = 0;
566         if (!from_memory) BLI_make_file_string(G.sce, tstr, home, ".B.blend");
567         strcpy(scestr, G.sce);  /* temporary store */
568         
569         /* prevent loading no UI */
570         G.fileflags &= ~G_FILE_NO_UI;
571         
572         /* put aside screens to match with persistant windows later */
573         wm_window_match_init(C, &wmbase); 
574         
575         if (!from_memory && BLI_exists(tstr)) {
576                 success = BKE_read_file(C, tstr, NULL, NULL);
577         } else {
578                 success = BKE_read_file_from_memory(C, datatoc_B_blend, datatoc_B_blend_size, NULL, NULL);
579         }
580         
581         /* match the read WM with current WM */
582         wm_window_match_do(C, &wmbase); 
583         wm_check(C); /* opens window(s), checks keymaps */
584
585         strcpy(G.sce, scestr); /* restore */
586
587         init_userdef_themes();
588         
589         /* When loading factory settings, the reset solid OpenGL lights need to be applied. */
590         GPU_default_lights();
591         
592         /* XXX */
593         G.save_over = 0;        // start with save preference untitled.blend
594         G.fileflags &= ~G_FILE_AUTOPLAY;        /*  disable autoplay in .B.blend... */
595 //      mainwindow_set_filename_to_title("");   // empty string re-initializes title to "Blender"
596
597         BLF_lang_init();
598         
599 //      refresh_interface_font();
600         
601 //      undo_editmode_clear();
602         BKE_reset_undo();
603         BKE_write_undo(C, "original");  /* save current state */
604         
605         WM_event_add_notifier(C, NC_WM|ND_FILEREAD, NULL);
606         CTX_wm_window_set(C, NULL); /* exits queues */
607                                    
608         return success;
609 }
610
611
612 static void get_autosave_location(char buf[FILE_MAXDIR+FILE_MAXFILE])
613 {
614         char pidstr[32];
615 #ifdef WIN32
616         char subdir[9];
617         char savedir[FILE_MAXDIR];
618 #endif
619
620         sprintf(pidstr, "%d.blend", abs(getpid()));
621         
622 #ifdef WIN32
623         if (!BLI_exists(U.tempdir)) {
624                 BLI_strncpy(subdir, "autosave", sizeof(subdir));
625                 BLI_make_file_string("/", savedir, BLI_gethome(), subdir);
626                 
627                 /* create a new autosave dir
628                  * function already checks for existence or not */
629                 BLI_recurdir_fileops(savedir);
630         
631                 BLI_make_file_string("/", buf, savedir, pidstr);
632                 return;
633         }
634 #endif
635         
636         BLI_make_file_string("/", buf, U.tempdir, pidstr);
637 }
638
639 void WM_read_autosavefile(bContext *C)
640 {
641         char tstr[FILE_MAX], scestr[FILE_MAX];
642         int save_over;
643
644         BLI_strncpy(scestr, G.sce, FILE_MAX);   /* temporal store */
645         
646         get_autosave_location(tstr);
647
648         save_over = G.save_over;
649         BKE_read_file(C, tstr, NULL, NULL);
650         G.save_over = save_over;
651         BLI_strncpy(G.sce, scestr, FILE_MAX);
652 }
653
654
655 void read_Blog(void)
656 {
657         char name[FILE_MAX], filename[FILE_MAX];
658         LinkNode *l, *lines;
659         struct RecentFile *recent;
660         char *line;
661         int num;
662
663         BLI_make_file_string("/", name, BLI_gethome(), ".Blog");
664         lines= BLI_read_file_as_lines(name);
665
666         G.recent_files.first = G.recent_files.last = NULL;
667
668         /* read list of recent opend files from .Blog to memory */
669         for (l= lines, num= 0; l && (num<U.recent_files); l= l->next, num++) {
670                 line = l->link;
671                 if (!BLI_streq(line, "")) {
672                         if (num==0) 
673                                 strcpy(G.sce, line);
674                         
675                         recent = (RecentFile*)MEM_mallocN(sizeof(RecentFile),"RecentFile");
676                         BLI_addtail(&(G.recent_files), recent);
677                         recent->filename = (char*)MEM_mallocN(sizeof(char)*(strlen(line)+1), "name of file");
678                         recent->filename[0] = '\0';
679                         
680                         strcpy(recent->filename, line);
681                 }
682         }
683
684         if(G.sce[0] == 0)
685                 BLI_make_file_string("/", G.sce, BLI_gethome(), "untitled.blend");
686         
687         BLI_free_file_lines(lines);
688
689 #ifdef WIN32
690         /* Add the drive names to the listing */
691         {
692                 __int64 tmp;
693                 char folder[MAX_PATH];
694                 char tmps[4];
695                 int i;
696                         
697                 tmp= GetLogicalDrives();
698                 
699                 for (i=2; i < 26; i++) {
700                         if ((tmp>>i) & 1) {
701                                 tmps[0]='a'+i;
702                                 tmps[1]=':';
703                                 tmps[2]='\\';
704                                 tmps[3]=0;
705                                 
706 // XX                           fsmenu_insert_entry(tmps, 0, 0);
707                         }
708                 }
709
710                 /* Adding Desktop and My Documents */
711 // XXX          fsmenu_append_separator();
712
713                 SHGetSpecialFolderPath(0, folder, CSIDL_PERSONAL, 0);
714 // XXX          fsmenu_insert_entry(folder, 0, 0);
715                 SHGetSpecialFolderPath(0, folder, CSIDL_DESKTOPDIRECTORY, 0);
716 // XXX          fsmenu_insert_entry(folder, 0, 0);
717
718 // XXX          fsmenu_append_separator();
719         }
720 #endif
721
722         BLI_make_file_string(G.sce, name, BLI_gethome(), ".Bfs");
723         lines= BLI_read_file_as_lines(name);
724
725         for (l= lines; l; l= l->next) {
726                 char *line= l->link;
727                         
728                 if (!BLI_streq(line, "")) {
729 // XXX                  fsmenu_insert_entry(line, 0, 1);
730                 }
731         }
732
733 // XXX  fsmenu_append_separator();
734         
735         /* add last saved file */
736         BLI_split_dirfile(G.sce, name, filename); /* G.sce shouldn't be relative */
737         
738 // XXX  fsmenu_insert_entry(name, 0, 0);
739         
740         BLI_free_file_lines(lines);
741 }
742
743 static void writeBlog(void)
744 {
745         struct RecentFile *recent, *next_recent;
746         char name[FILE_MAXDIR+FILE_MAXFILE];
747         FILE *fp;
748         int i;
749
750         BLI_make_file_string("/", name, BLI_gethome(), ".Blog");
751
752         recent = G.recent_files.first;
753         /* refresh .Blog of recent opened files, when current file was changed */
754         if(!(recent) || (strcmp(recent->filename, G.sce)!=0)) {
755                 fp= fopen(name, "w");
756                 if (fp) {
757                         /* add current file to the beginning of list */
758                         recent = (RecentFile*)MEM_mallocN(sizeof(RecentFile),"RecentFile");
759                         recent->filename = (char*)MEM_mallocN(sizeof(char)*(strlen(G.sce)+1), "name of file");
760                         recent->filename[0] = '\0';
761                         strcpy(recent->filename, G.sce);
762                         BLI_addhead(&(G.recent_files), recent);
763                         /* write current file to .Blog */
764                         fprintf(fp, "%s\n", recent->filename);
765                         recent = recent->next;
766                         i=1;
767                         /* write rest of recent opened files to .Blog */
768                         while((i<U.recent_files) && (recent)){
769                                 /* this prevents to have duplicities in list */
770                                 if (strcmp(recent->filename, G.sce)!=0) {
771                                         fprintf(fp, "%s\n", recent->filename);
772                                         recent = recent->next;
773                                 }
774                                 else {
775                                         next_recent = recent->next;
776                                         MEM_freeN(recent->filename);
777                                         BLI_freelinkN(&(G.recent_files), recent);
778                                         recent = next_recent;
779                                 }
780                                 i++;
781                         }
782                         fclose(fp);
783                 }
784         }
785 }
786
787 static void do_history(char *name, ReportList *reports)
788 {
789         char tempname1[FILE_MAXDIR+FILE_MAXFILE], tempname2[FILE_MAXDIR+FILE_MAXFILE];
790         int hisnr= U.versions;
791         
792         if(U.versions==0) return;
793         if(strlen(name)<2) return;
794                 
795         while(hisnr > 1) {
796                 sprintf(tempname1, "%s%d", name, hisnr-1);
797                 sprintf(tempname2, "%s%d", name, hisnr);
798         
799                 if(BLI_rename(tempname1, tempname2))
800                         BKE_report(reports, RPT_ERROR, "Unable to make version backup");
801                         
802                 hisnr--;
803         }
804                 
805         /* is needed when hisnr==1 */
806         sprintf(tempname1, "%s%d", name, hisnr);
807         
808         if(BLI_rename(name, tempname1))
809                 BKE_report(reports, RPT_ERROR, "Unable to make version backup");
810 }
811
812 void WM_write_file(bContext *C, char *target, ReportList *reports)
813 {
814         Library *li;
815         int writeflags, len;
816         char di[FILE_MAX];
817         
818         len = strlen(target);
819         
820         if (len == 0) return;
821         if (len >= FILE_MAX) {
822                 BKE_report(reports, RPT_ERROR, "Path too long, cannot save");
823                 return;
824         }
825  
826         /* send the OnSave event */
827 // XXX  if (G.f & G_DOSCRIPTLINKS) BPY_do_pyscript(&CTX_data_scene(C)->id, SCRIPT_ONSAVE);
828
829         for (li= G.main->library.first; li; li= li->id.next) {
830                 if (BLI_streq(li->name, target)) {
831                         BKE_report(reports, RPT_ERROR, "Cannot overwrite used library");
832                         return;
833                 }
834         }
835         
836         if (!BLO_has_bfile_extension(target) && (len+6 < FILE_MAX)) {
837                 sprintf(di, "%s.blend", target);
838         } else {
839                 strcpy(di, target);
840         }
841
842         if (BLI_exists(di)) {
843 // XXX          if(!saveover(di))
844 // XXX                  return; 
845         }
846         
847         if (G.fileflags & G_AUTOPACK) {
848                 packAll();
849         }
850         
851 // XXX  waitcursor(1);  // exit_editmode sets cursor too
852
853         do_history(di, reports);
854         
855         /* we use the UserDef to define compression flag */
856         writeflags= G.fileflags & ~G_FILE_COMPRESS;
857         if(U.flag & USER_FILECOMPRESS)
858                 writeflags |= G_FILE_COMPRESS;
859         
860         if (BLO_write_file(CTX_data_main(C), di, writeflags, reports)) {
861                 strcpy(G.sce, di);
862                 G.relbase_valid = 1;
863                 strcpy(G.main->name, di);       /* is guaranteed current file */
864
865 // XXX          mainwindow_set_filename_to_title(G.main->name);
866
867                 G.save_over = 1;
868
869                 writeBlog();
870         }
871
872 // XXX  waitcursor(0);
873 }
874
875 /* operator entry */
876 int WM_write_homefile(bContext *C, wmOperator *op)
877 {
878         char tstr[FILE_MAXDIR+FILE_MAXFILE];
879         int write_flags;
880         
881         BLI_make_file_string("/", tstr, BLI_gethome(), ".B.blend");
882                 
883         /*  force save as regular blend file */
884         write_flags = G.fileflags & ~(G_FILE_COMPRESS | G_FILE_LOCK | G_FILE_SIGN);
885
886         BLO_write_file(CTX_data_main(C), tstr, write_flags, op->reports);
887         
888         return OPERATOR_FINISHED;
889 }
890
891 void WM_write_autosave(bContext *C)
892 {
893         char tstr[FILE_MAXDIR+FILE_MAXFILE];
894         int write_flags;
895         
896         get_autosave_location(tstr);
897
898                 /*  force save as regular blend file */
899         write_flags = G.fileflags & ~(G_FILE_COMPRESS | G_FILE_LOCK | G_FILE_SIGN);
900
901                 /* error reporting to console */
902         BLO_write_file(CTX_data_main(C), tstr, write_flags, NULL);
903 }
904
905 /* if global undo; remove tempsave, otherwise rename */
906 void delete_autosave(void)
907 {
908         char tstr[FILE_MAXDIR+FILE_MAXFILE];
909         
910         get_autosave_location(tstr);
911
912         if (BLI_exists(tstr)) {
913                 char str[FILE_MAXDIR+FILE_MAXFILE];
914                 BLI_make_file_string("/", str, U.tempdir, "quit.blend");
915
916                 if(U.uiflag & USER_GLOBALUNDO) BLI_delete(tstr, 0, 0);
917                 else BLI_rename(tstr, str);
918         }
919 }
920
921 /***/
922