2.50: svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r18677...
[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
88 #include "GPU_draw.h"
89
90 // XXX #include "BPY_extern.h"
91
92 #include "WM_api.h"
93 #include "WM_types.h"
94 #include "wm.h"
95 #include "wm_window.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 < 245) || (G.main->versionfile == 245 && G.main->subversionfile < 11)) {
329                 bTheme *btheme;
330                 for (btheme= U.themes.first; btheme; btheme= btheme->next) {
331                         /* these should all use the same color */
332                         SETCOL(btheme->tv3d.cframe, 0x60, 0xc0, 0x40, 255);
333                         SETCOL(btheme->tipo.cframe, 0x60, 0xc0, 0x40, 255);
334                         SETCOL(btheme->tact.cframe, 0x60, 0xc0, 0x40, 255);
335                         SETCOL(btheme->tnla.cframe, 0x60, 0xc0, 0x40, 255);
336                         SETCOL(btheme->tseq.cframe, 0x60, 0xc0, 0x40, 255);
337                         SETCOL(btheme->tsnd.cframe, 0x60, 0xc0, 0x40, 255);
338                         SETCOL(btheme->ttime.cframe, 0x60, 0xc0, 0x40, 255);
339                 }
340         }
341         if ((G.main->versionfile < 245) || (G.main->versionfile == 245 && G.main->subversionfile < 13)) {
342                 bTheme *btheme;
343                 for (btheme= U.themes.first; btheme; btheme= btheme->next) {
344                         /* action channel groups (recolor anyway) */
345                         SETCOL(btheme->tact.group, 0x39, 0x7d, 0x1b, 255);
346                         SETCOL(btheme->tact.group_active, 0x7d, 0xe9, 0x60, 255);
347                         
348                         /* bone custom-color sets */
349                         // FIXME: this check for initialised colors is bad
350                         if (btheme->tarm[0].solid[3] == 0) {
351                                         /* set 1 */
352                                 SETCOL(btheme->tarm[0].solid, 0x9a, 0x00, 0x00, 255);
353                                 SETCOL(btheme->tarm[0].select, 0xbd, 0x11, 0x11, 255);
354                                 SETCOL(btheme->tarm[0].active, 0xf7, 0x0a, 0x0a, 255);
355                                         /* set 2 */
356                                 SETCOL(btheme->tarm[1].solid, 0xf7, 0x40, 0x18, 255);
357                                 SETCOL(btheme->tarm[1].select, 0xf6, 0x69, 0x13, 255);
358                                 SETCOL(btheme->tarm[1].active, 0xfa, 0x99, 0x00, 255);
359                                         /* set 3 */
360                                 SETCOL(btheme->tarm[2].solid, 0x1e, 0x91, 0x09, 255);
361                                 SETCOL(btheme->tarm[2].select, 0x59, 0xb7, 0x0b, 255);
362                                 SETCOL(btheme->tarm[2].active, 0x83, 0xef, 0x1d, 255);
363                                         /* set 4 */
364                                 SETCOL(btheme->tarm[3].solid, 0x0a, 0x36, 0x94, 255);
365                                 SETCOL(btheme->tarm[3].select, 0x36, 0x67, 0xdf, 255);
366                                 SETCOL(btheme->tarm[3].active, 0x5e, 0xc1, 0xef, 255);
367                                         /* set 5 */
368                                 SETCOL(btheme->tarm[4].solid, 0xa9, 0x29, 0x4e, 255);
369                                 SETCOL(btheme->tarm[4].select, 0xc1, 0x41, 0x6a, 255);
370                                 SETCOL(btheme->tarm[4].active, 0xf0, 0x5d, 0x91, 255);
371                                         /* set 6 */
372                                 SETCOL(btheme->tarm[5].solid, 0x43, 0x0c, 0x78, 255);
373                                 SETCOL(btheme->tarm[5].select, 0x54, 0x3a, 0xa3, 255);
374                                 SETCOL(btheme->tarm[5].active, 0x87, 0x64, 0xd5, 255);
375                                         /* set 7 */
376                                 SETCOL(btheme->tarm[6].solid, 0x24, 0x78, 0x5a, 255);
377                                 SETCOL(btheme->tarm[6].select, 0x3c, 0x95, 0x79, 255);
378                                 SETCOL(btheme->tarm[6].active, 0x6f, 0xb6, 0xab, 255);
379                                         /* set 8 */
380                                 SETCOL(btheme->tarm[7].solid, 0x4b, 0x70, 0x7c, 255);
381                                 SETCOL(btheme->tarm[7].select, 0x6a, 0x86, 0x91, 255);
382                                 SETCOL(btheme->tarm[7].active, 0x9b, 0xc2, 0xcd, 255);
383                                         /* set 9 */
384                                 SETCOL(btheme->tarm[8].solid, 0xf4, 0xc9, 0x0c, 255);
385                                 SETCOL(btheme->tarm[8].select, 0xee, 0xc2, 0x36, 255);
386                                 SETCOL(btheme->tarm[8].active, 0xf3, 0xff, 0x00, 255);
387                                         /* set 10 */
388                                 SETCOL(btheme->tarm[9].solid, 0x1e, 0x20, 0x24, 255);
389                                 SETCOL(btheme->tarm[9].select, 0x48, 0x4c, 0x56, 255);
390                                 SETCOL(btheme->tarm[9].active, 0xff, 0xff, 0xff, 255);
391                                         /* set 11 */
392                                 SETCOL(btheme->tarm[10].solid, 0x6f, 0x2f, 0x6a, 255);
393                                 SETCOL(btheme->tarm[10].select, 0x98, 0x45, 0xbe, 255);
394                                 SETCOL(btheme->tarm[10].active, 0xd3, 0x30, 0xd6, 255);
395                                         /* set 12 */
396                                 SETCOL(btheme->tarm[11].solid, 0x6c, 0x8e, 0x22, 255);
397                                 SETCOL(btheme->tarm[11].select, 0x7f, 0xb0, 0x22, 255);
398                                 SETCOL(btheme->tarm[11].active, 0xbb, 0xef, 0x5b, 255);
399                                         /* set 13 */
400                                 SETCOL(btheme->tarm[12].solid, 0x8d, 0x8d, 0x8d, 255);
401                                 SETCOL(btheme->tarm[12].select, 0xb0, 0xb0, 0xb0, 255);
402                                 SETCOL(btheme->tarm[12].active, 0xde, 0xde, 0xde, 255);
403                                         /* set 14 */
404                                 SETCOL(btheme->tarm[13].solid, 0x83, 0x43, 0x26, 255);
405                                 SETCOL(btheme->tarm[13].select, 0x8b, 0x58, 0x11, 255);
406                                 SETCOL(btheme->tarm[13].active, 0xbd, 0x6a, 0x11, 255);
407                                         /* set 15 */
408                                 SETCOL(btheme->tarm[14].solid, 0x08, 0x31, 0x0e, 255);
409                                 SETCOL(btheme->tarm[14].select, 0x1c, 0x43, 0x0b, 255);
410                                 SETCOL(btheme->tarm[14].active, 0x34, 0x62, 0x2b, 255);
411                         }
412                 }
413         }
414         if ((G.main->versionfile < 245) || (G.main->versionfile == 245 && G.main->subversionfile < 16)) {
415                 U.flag |= USER_ADD_VIEWALIGNED|USER_ADD_EDITMODE;
416         }
417         if ((G.main->versionfile < 247) || (G.main->versionfile == 247 && G.main->subversionfile <= 2)) {
418                 bTheme *btheme;
419                 
420                 /* adjust themes */
421                 for (btheme= U.themes.first; btheme; btheme= btheme->next) {
422                         char *col;
423                         
424                         /* IPO Editor: Handles/Vertices */
425                         col = btheme->tipo.vertex;
426                         SETCOL(btheme->tipo.handle_vertex, col[0], col[1], col[2], 255);
427                         col = btheme->tipo.vertex_select;
428                         SETCOL(btheme->tipo.handle_vertex_select, col[0], col[1], col[2], 255);
429                         btheme->tipo.handle_vertex_size= btheme->tipo.vertex_size;
430                         
431                         /* Sequence/Image Editor: colors for GPencil text */
432                         col = btheme->tv3d.bone_pose;
433                         SETCOL(btheme->tseq.bone_pose, col[0], col[1], col[2], 255);
434                         SETCOL(btheme->tima.bone_pose, col[0], col[1], col[2], 255);
435                         col = btheme->tv3d.vertex_select;
436                         SETCOL(btheme->tseq.vertex_select, col[0], col[1], col[2], 255);
437                 }
438         }
439         if (G.main->versionfile < 250) {
440                 bTheme *btheme;
441                 
442                 for(btheme= U.themes.first; btheme; btheme= btheme->next) {
443                         /* this was not properly initialized in 2.45 */
444                         if(btheme->tima.face_dot[3]==0) {
445                                 SETCOL(btheme->tima.editmesh_active, 255, 255, 255, 128);
446                                 SETCOL(btheme->tima.face_dot, 255, 133, 0, 255);
447                                 btheme->tima.facedot_size= 2;
448                         }
449                         
450                         /* DopeSheet - (Object) Channel color */
451                         SETCOL(btheme->tact.ds_channel,         82, 96, 110, 255);
452                         SETCOL(btheme->tact.ds_subchannel,      124, 137, 150, 255);
453                         /* DopeSheet - Group Channel color (saner version) */
454                         SETCOL(btheme->tact.group, 79, 101, 73, 255);
455                         SETCOL(btheme->tact.group_active, 135, 177, 125, 255);
456                         
457                         /* Graph Editor - (Object) Channel color */
458                         SETCOL(btheme->tipo.ds_channel,         82, 96, 110, 255);
459                         SETCOL(btheme->tipo.ds_subchannel,      124, 137, 150, 255);
460                         /* Graph Editor - Group Channel color */
461                         SETCOL(btheme->tipo.group, 79, 101, 73, 255);
462                         SETCOL(btheme->tipo.group_active, 135, 177, 125, 255);
463                 }
464                 
465                 /* adjust grease-pencil distances */
466                 U.gp_manhattendist= 1;
467                 U.gp_euclideandist= 2;
468                 
469                 /* adjust default interpolation for new IPO-curves */
470                 U.ipo_new= BEZT_IPO_BEZ;
471         }
472         
473         /* GL Texture Garbage Collection (variable abused above!) */
474         if (U.textimeout == 0) {
475                 U.texcollectrate = 60;
476                 U.textimeout = 120;
477         }
478         if (U.memcachelimit <= 0) {
479                 U.memcachelimit = 32;
480         }
481         if (U.frameserverport == 0) {
482                 U.frameserverport = 8080;
483         }
484
485         MEM_CacheLimiter_set_maximum(U.memcachelimit * 1024 * 1024);
486         
487         // sets themes, fonts, .. from userdef
488         UI_init_userdef();
489         
490         /* funny name, but it is GE stuff, moves userdef stuff to engine */
491 // XXX  space_set_commmandline_options();
492         /* this timer uses U */
493 // XXX  reset_autosave();
494
495 }
496
497 /* To be able to read files without windows closing, opening, moving 
498    we try to prepare for worst case:
499    - active window gets active screen from file 
500    - restoring the screens from non-active windows 
501    Best case is all screens match, in that case they get assigned to proper window  
502 */
503 static void wm_window_match_init(bContext *C, ListBase *wmlist)
504 {
505         wmWindowManager *wm= G.main->wm.first;
506         wmWindow *win;
507         
508         *wmlist= G.main->wm;
509         G.main->wm.first= G.main->wm.last= NULL;
510         
511         /* first wrap up running stuff */
512         /* code copied from wm_init_exit.c */
513         for(wm= wmlist->first; wm; wm= wm->id.next) {
514                 
515                 WM_jobs_stop_all(wm);
516                 
517                 for(win= wm->windows.first; win; win= win->next) {
518                 
519                         CTX_wm_window_set(C, win);      /* needed by operator close callbacks */
520                         ED_screen_exit(C, win, win->screen);
521                 }
522         }
523         
524         ED_editors_exit(C);
525         
526 return; 
527         if(wm==NULL) return;
528         if(G.fileflags & G_FILE_NO_UI) return;
529         
530         /* we take apart the used screens from non-active window */
531         for(win= wm->windows.first; win; win= win->next) {
532                 BLI_strncpy(win->screenname, win->screen->id.name, MAX_ID_NAME);
533                 if(win!=wm->winactive) {
534                         BLI_remlink(&G.main->screen, win->screen);
535                         //BLI_addtail(screenbase, win->screen);
536                 }
537         }
538 }
539
540 /* match old WM with new, 4 cases:
541   1- no current wm, no read wm: make new default
542   2- no current wm, but read wm: that's OK, do nothing
543   3- current wm, but not in file: try match screen names
544   4- current wm, and wm in file: try match ghostwin 
545 */
546
547 static void wm_window_match_do(bContext *C, ListBase *oldwmlist)
548 {
549         wmWindowManager *oldwm, *wm;
550         wmWindow *oldwin, *win;
551         
552         /* cases 1 and 2 */
553         if(oldwmlist->first==NULL) {
554                 if(G.main->wm.first); /* nothing todo */
555                 else
556                         wm_add_default(C);
557         }
558         else {
559                 /* cases 3 and 4 */
560                 
561                 /* we've read file without wm..., keep current one entirely alive */
562                 if(G.main->wm.first==NULL) {
563                         bScreen *screen= CTX_wm_screen(C);
564                         
565                         /* match oldwm to new dbase, only old files */
566                         
567                         for(wm= oldwmlist->first; wm; wm= wm->id.next) {
568                                 
569                                 for(win= wm->windows.first; win; win= win->next) {
570                                         /* all windows get active screen from file */
571                                         if(screen->winid==0)
572                                                 win->screen= screen;
573                                         else 
574                                                 win->screen= ED_screen_duplicate(win, screen);
575                                         
576                                         BLI_strncpy(win->screenname, win->screen->id.name+2, 21);
577                                         win->screen->winid= win->winid;
578                                 }
579                         }
580                         
581                         G.main->wm= *oldwmlist;
582                         
583                         /* screens were read from file! */
584                         ED_screens_initialize(G.main->wm.first);
585                 }
586                 else {
587                         /* what if old was 3, and loaded 1? */
588                         /* this code could move to setup_appdata */
589                         oldwm= oldwmlist->first;
590                         wm= G.main->wm.first;
591
592                         /* ensure making new keymaps and set space types */
593                         wm->initialized= 0;
594                         
595                         /* only first wm in list has ghostwins */
596                         for(win= wm->windows.first; win; win= win->next) {
597                                 for(oldwin= oldwm->windows.first; oldwin; oldwin= oldwin->next) {
598                                         
599                                         if(oldwin->winid == win->winid ) {
600                                                 win->ghostwin= oldwin->ghostwin;
601                                                 GHOST_SetWindowUserData(win->ghostwin, win);    /* pointer back */
602                                                 oldwin->ghostwin= NULL;
603                                                 
604                                                 win->eventstate= oldwin->eventstate;
605                                                 oldwin->eventstate= NULL;
606                                         }
607                                 }
608                         }
609                         wm_close_and_free_all(C, oldwmlist);
610                 }
611         }
612 }
613
614 void WM_read_file(bContext *C, char *name, ReportList *reports)
615 {
616         int retval;
617
618         /* first try to append data from exotic file formats... */
619         /* it throws error box when file doesnt exist and returns -1 */
620         /* note; it should set some error message somewhere... (ton) */
621         retval= BKE_read_exotic(CTX_data_scene(C), name);
622         
623         /* we didn't succeed, now try to read Blender file */
624         if (retval== 0) {
625                 ListBase wmbase;
626
627                 /* put aside screens to match with persistant windows later */
628                 /* also exit screens and editors */
629                 wm_window_match_init(C, &wmbase); 
630                 
631                 retval= BKE_read_file(C, name, NULL, reports);
632                 G.save_over = 1;
633
634                 /* match the read WM with current WM */
635                 wm_window_match_do(C, &wmbase); 
636                 wm_check(C); /* opens window(s), checks keymaps */
637                 
638 // XXX          mainwindow_set_filename_to_title(G.main->name);
639 // XXX          sound_initialize_sounds();
640
641                 if(retval==2) init_userdef_themes();    // in case a userdef is read from regular .blend
642                 
643                 if (retval!=0) G.relbase_valid = 1;
644
645 // XXX          undo_editmode_clear();
646                 BKE_reset_undo();
647                 BKE_write_undo(C, "original");  /* save current state */
648
649                 WM_event_add_notifier(C, NC_WM|ND_FILEREAD, NULL);
650 //              refresh_interface_font();
651                                            
652                 CTX_wm_window_set(C, NULL); /* exits queues */
653         }
654         else if(retval==1)
655                 BKE_write_undo(C, "Import file");
656         else if(retval == -1) {
657                 if(reports && reports->list.first == NULL)
658                         BKE_report(reports, RPT_ERROR, "Cannot read file.");
659         }
660 }
661
662
663 /* called on startup,  (context entirely filled with NULLs) */
664 /* or called for 'Erase All' */
665 int WM_read_homefile(bContext *C, wmOperator *op)
666 {
667         ListBase wmbase;
668         char tstr[FILE_MAXDIR+FILE_MAXFILE], scestr[FILE_MAXDIR];
669         char *home= BLI_gethome();
670         int from_memory= op?RNA_boolean_get(op->ptr, "factory"):0;
671         int success;
672                 
673         BLI_clean(home);
674         
675         free_ttfont(); /* still weird... what does it here? */
676                 
677         G.relbase_valid = 0;
678         if (!from_memory) BLI_make_file_string(G.sce, tstr, home, ".B.blend");
679         strcpy(scestr, G.sce);  /* temporary store */
680         
681         /* prevent loading no UI */
682         G.fileflags &= ~G_FILE_NO_UI;
683         
684         /* put aside screens to match with persistant windows later */
685         wm_window_match_init(C, &wmbase); 
686         
687         if (!from_memory && BLI_exists(tstr)) {
688                 success = BKE_read_file(C, tstr, NULL, NULL);
689         } else {
690                 success = BKE_read_file_from_memory(C, datatoc_B_blend, datatoc_B_blend_size, NULL, NULL);
691         }
692         
693         /* match the read WM with current WM */
694         wm_window_match_do(C, &wmbase); 
695         wm_check(C); /* opens window(s), checks keymaps */
696
697         strcpy(G.sce, scestr); /* restore */
698         
699         init_userdef_themes();
700         
701         /* When loading factory settings, the reset solid OpenGL lights need to be applied. */
702         GPU_default_lights();
703         
704         /* XXX */
705         G.save_over = 0;        // start with save preference untitled.blend
706         G.fileflags &= ~G_FILE_AUTOPLAY;        /*  disable autoplay in .B.blend... */
707 //      mainwindow_set_filename_to_title("");   // empty string re-initializes title to "Blender"
708         
709 //      refresh_interface_font();
710         
711 //      undo_editmode_clear();
712         BKE_reset_undo();
713         BKE_write_undo(C, "original");  /* save current state */
714         
715         WM_event_add_notifier(C, NC_WM|ND_FILEREAD, NULL);
716         CTX_wm_window_set(C, NULL); /* exits queues */
717                                    
718         return success;
719 }
720
721
722 static void get_autosave_location(char buf[FILE_MAXDIR+FILE_MAXFILE])
723 {
724         char pidstr[32];
725 #ifdef WIN32
726         char subdir[9];
727         char savedir[FILE_MAXDIR];
728 #endif
729
730         sprintf(pidstr, "%d.blend", abs(getpid()));
731         
732 #ifdef WIN32
733         if (!BLI_exists(U.tempdir)) {
734                 BLI_strncpy(subdir, "autosave", sizeof(subdir));
735                 BLI_make_file_string("/", savedir, BLI_gethome(), subdir);
736                 
737                 /* create a new autosave dir
738                  * function already checks for existence or not */
739                 BLI_recurdir_fileops(savedir);
740         
741                 BLI_make_file_string("/", buf, savedir, pidstr);
742                 return;
743         }
744 #endif
745         
746         BLI_make_file_string("/", buf, U.tempdir, pidstr);
747 }
748
749 void WM_read_autosavefile(bContext *C)
750 {
751         char tstr[FILE_MAX], scestr[FILE_MAX];
752         int save_over;
753
754         BLI_strncpy(scestr, G.sce, FILE_MAX);   /* temporal store */
755         
756         get_autosave_location(tstr);
757
758         save_over = G.save_over;
759         BKE_read_file(C, tstr, NULL, NULL);
760         G.save_over = save_over;
761         BLI_strncpy(G.sce, scestr, FILE_MAX);
762 }
763
764
765 void read_Blog(void)
766 {
767         char name[FILE_MAX], filename[FILE_MAX];
768         LinkNode *l, *lines;
769         struct RecentFile *recent;
770         char *line;
771         int num;
772
773         BLI_make_file_string("/", name, BLI_gethome(), ".Blog");
774         lines= BLI_read_file_as_lines(name);
775
776         G.recent_files.first = G.recent_files.last = NULL;
777
778         /* read list of recent opend files from .Blog to memory */
779         for (l= lines, num= 0; l && (num<U.recent_files); l= l->next, num++) {
780                 line = l->link;
781                 if (!BLI_streq(line, "")) {
782                         if (num==0) 
783                                 strcpy(G.sce, line);
784                         
785                         recent = (RecentFile*)MEM_mallocN(sizeof(RecentFile),"RecentFile");
786                         BLI_addtail(&(G.recent_files), recent);
787                         recent->filename = (char*)MEM_mallocN(sizeof(char)*(strlen(line)+1), "name of file");
788                         recent->filename[0] = '\0';
789                         
790                         strcpy(recent->filename, line);
791                 }
792         }
793
794         if(G.sce[0] == 0)
795                 BLI_make_file_string("/", G.sce, BLI_gethome(), "untitled.blend");
796         
797         BLI_free_file_lines(lines);
798
799 #ifdef WIN32
800         /* Add the drive names to the listing */
801         {
802                 __int64 tmp;
803                 char folder[MAX_PATH];
804                 char tmps[4];
805                 int i;
806                         
807                 tmp= GetLogicalDrives();
808                 
809                 for (i=2; i < 26; i++) {
810                         if ((tmp>>i) & 1) {
811                                 tmps[0]='a'+i;
812                                 tmps[1]=':';
813                                 tmps[2]='\\';
814                                 tmps[3]=0;
815                                 
816 // XX                           fsmenu_insert_entry(tmps, 0, 0);
817                         }
818                 }
819
820                 /* Adding Desktop and My Documents */
821 // XXX          fsmenu_append_separator();
822
823                 SHGetSpecialFolderPath(0, folder, CSIDL_PERSONAL, 0);
824 // XXX          fsmenu_insert_entry(folder, 0, 0);
825                 SHGetSpecialFolderPath(0, folder, CSIDL_DESKTOPDIRECTORY, 0);
826 // XXX          fsmenu_insert_entry(folder, 0, 0);
827
828 // XXX          fsmenu_append_separator();
829         }
830 #endif
831
832         BLI_make_file_string(G.sce, name, BLI_gethome(), ".Bfs");
833         lines= BLI_read_file_as_lines(name);
834
835         for (l= lines; l; l= l->next) {
836                 char *line= l->link;
837                         
838                 if (!BLI_streq(line, "")) {
839 // XXX                  fsmenu_insert_entry(line, 0, 1);
840                 }
841         }
842
843 // XXX  fsmenu_append_separator();
844         
845         /* add last saved file */
846         BLI_split_dirfile(G.sce, name, filename); /* G.sce shouldn't be relative */
847         
848 // XXX  fsmenu_insert_entry(name, 0, 0);
849         
850         BLI_free_file_lines(lines);
851 }
852
853 static void writeBlog(void)
854 {
855         struct RecentFile *recent, *next_recent;
856         char name[FILE_MAXDIR+FILE_MAXFILE];
857         FILE *fp;
858         int i;
859
860         BLI_make_file_string("/", name, BLI_gethome(), ".Blog");
861
862         recent = G.recent_files.first;
863         /* refresh .Blog of recent opened files, when current file was changed */
864         if(!(recent) || (strcmp(recent->filename, G.sce)!=0)) {
865                 fp= fopen(name, "w");
866                 if (fp) {
867                         /* add current file to the beginning of list */
868                         recent = (RecentFile*)MEM_mallocN(sizeof(RecentFile),"RecentFile");
869                         recent->filename = (char*)MEM_mallocN(sizeof(char)*(strlen(G.sce)+1), "name of file");
870                         recent->filename[0] = '\0';
871                         strcpy(recent->filename, G.sce);
872                         BLI_addhead(&(G.recent_files), recent);
873                         /* write current file to .Blog */
874                         fprintf(fp, "%s\n", recent->filename);
875                         recent = recent->next;
876                         i=1;
877                         /* write rest of recent opened files to .Blog */
878                         while((i<U.recent_files) && (recent)){
879                                 /* this prevents to have duplicities in list */
880                                 if (strcmp(recent->filename, G.sce)!=0) {
881                                         fprintf(fp, "%s\n", recent->filename);
882                                         recent = recent->next;
883                                 }
884                                 else {
885                                         next_recent = recent->next;
886                                         MEM_freeN(recent->filename);
887                                         BLI_freelinkN(&(G.recent_files), recent);
888                                         recent = next_recent;
889                                 }
890                                 i++;
891                         }
892                         fclose(fp);
893                 }
894         }
895 }
896
897 static void do_history(char *name, ReportList *reports)
898 {
899         char tempname1[FILE_MAXDIR+FILE_MAXFILE], tempname2[FILE_MAXDIR+FILE_MAXFILE];
900         int hisnr= U.versions;
901         
902         if(U.versions==0) return;
903         if(strlen(name)<2) return;
904                 
905         while(hisnr > 1) {
906                 sprintf(tempname1, "%s%d", name, hisnr-1);
907                 sprintf(tempname2, "%s%d", name, hisnr);
908         
909                 if(BLI_rename(tempname1, tempname2))
910                         BKE_report(reports, RPT_ERROR, "Unable to make version backup");
911                         
912                 hisnr--;
913         }
914                 
915         /* is needed when hisnr==1 */
916         sprintf(tempname1, "%s%d", name, hisnr);
917         
918         if(BLI_rename(name, tempname1))
919                 BKE_report(reports, RPT_ERROR, "Unable to make version backup");
920 }
921
922 void WM_write_file(bContext *C, char *target, ReportList *reports)
923 {
924         Library *li;
925         int writeflags, len;
926         char di[FILE_MAX];
927         
928         len = strlen(target);
929         
930         if (len == 0) return;
931         if (len >= FILE_MAX) {
932                 BKE_report(reports, RPT_ERROR, "Path too long, cannot save");
933                 return;
934         }
935  
936         /* send the OnSave event */
937 // XXX  if (G.f & G_DOSCRIPTLINKS) BPY_do_pyscript(&CTX_data_scene(C)->id, SCRIPT_ONSAVE);
938
939         for (li= G.main->library.first; li; li= li->id.next) {
940                 if (BLI_streq(li->name, target)) {
941                         BKE_report(reports, RPT_ERROR, "Cannot overwrite used library");
942                         return;
943                 }
944         }
945         
946         if (!BLO_has_bfile_extension(target) && (len+6 < FILE_MAX)) {
947                 sprintf(di, "%s.blend", target);
948         } else {
949                 strcpy(di, target);
950         }
951
952         if (BLI_exists(di)) {
953 // XXX          if(!saveover(di))
954 // XXX                  return; 
955         }
956         
957         if (G.fileflags & G_AUTOPACK) {
958                 packAll();
959         }
960         
961 // XXX  waitcursor(1);  // exit_editmode sets cursor too
962
963         do_history(di, reports);
964         
965         /* we use the UserDef to define compression flag */
966         writeflags= G.fileflags & ~G_FILE_COMPRESS;
967         if(U.flag & USER_FILECOMPRESS)
968                 writeflags |= G_FILE_COMPRESS;
969         
970         if (BLO_write_file(CTX_data_main(C), di, writeflags, reports)) {
971                 strcpy(G.sce, di);
972                 G.relbase_valid = 1;
973                 strcpy(G.main->name, di);       /* is guaranteed current file */
974
975                 G.save_over = 1; /* disable untitled.blend convention */
976                 
977                 writeBlog();
978         }
979
980 // XXX  waitcursor(0);
981 }
982
983 /* operator entry */
984 int WM_write_homefile(bContext *C, wmOperator *op)
985 {
986         char tstr[FILE_MAXDIR+FILE_MAXFILE];
987         int write_flags;
988         
989         BLI_make_file_string("/", tstr, BLI_gethome(), ".B.blend");
990                 
991         /*  force save as regular blend file */
992         write_flags = G.fileflags & ~(G_FILE_COMPRESS | G_FILE_LOCK | G_FILE_SIGN);
993
994         BLO_write_file(CTX_data_main(C), tstr, write_flags, op->reports);
995         
996         G.save_over= 0;
997         
998         return OPERATOR_FINISHED;
999 }
1000
1001 void WM_write_autosave(bContext *C)
1002 {
1003         char tstr[FILE_MAXDIR+FILE_MAXFILE];
1004         int write_flags;
1005         
1006         get_autosave_location(tstr);
1007
1008                 /*  force save as regular blend file */
1009         write_flags = G.fileflags & ~(G_FILE_COMPRESS | G_FILE_LOCK | G_FILE_SIGN);
1010
1011                 /* error reporting to console */
1012         BLO_write_file(CTX_data_main(C), tstr, write_flags, NULL);
1013 }
1014
1015 /* if global undo; remove tempsave, otherwise rename */
1016 void delete_autosave(void)
1017 {
1018         char tstr[FILE_MAXDIR+FILE_MAXFILE];
1019         
1020         get_autosave_location(tstr);
1021
1022         if (BLI_exists(tstr)) {
1023                 char str[FILE_MAXDIR+FILE_MAXFILE];
1024                 BLI_make_file_string("/", str, U.tempdir, "quit.blend");
1025
1026                 if(U.uiflag & USER_GLOBALUNDO) BLI_delete(tstr, 0, 0);
1027                 else BLI_rename(tstr, str);
1028         }
1029 }
1030
1031 /***/
1032