svn merge -r 15392:15551 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[blender.git] / source / blender / src / usiblender.c
1 /**
2  * $Id$
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  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30         /* placed up here because of crappy
31          * winsock stuff.
32          */
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36
37 #include "GL/glew.h"
38
39 #ifdef WIN32
40 #include <windows.h> /* need to include windows.h so _WIN32_IE is defined  */
41 #ifndef _WIN32_IE
42 #define _WIN32_IE 0x0400 /* minimal requirements for SHGetSpecialFolderPath on MINGW MSVC has this defined already */
43 #endif
44 #include <shlobj.h> /* for SHGetSpecialFolderPath, has to be done before BLI_winstuff because 'near' is disabled through BLI_windstuff */
45 #include "BLI_winstuff.h"
46 #include <process.h> /* getpid */
47 #else
48 #include <unistd.h> /* getpid */
49 #endif
50 #include "MEM_guardedalloc.h"
51 #include "MEM_CacheLimiterC-Api.h"
52
53 #include "BMF_Api.h"
54 #include "BIF_language.h"
55 #ifdef INTERNATIONAL
56 #include "FTF_Api.h"
57 #endif
58
59 #include "BLI_blenlib.h"
60 #include "BLI_arithb.h"
61 #include "BLI_linklist.h"
62
63 #include "IMB_imbuf_types.h"
64 #include "IMB_imbuf.h"
65
66 #include "DNA_object_types.h"
67 #include "DNA_space_types.h"
68 #include "DNA_userdef_types.h"
69 #include "DNA_sound_types.h"
70 #include "DNA_scene_types.h"
71 #include "DNA_screen_types.h"
72
73 #include "BKE_blender.h"
74 #include "BKE_curve.h"
75 #include "BKE_displist.h"
76 #include "BKE_DerivedMesh.h"
77 #include "BKE_exotic.h"
78 #include "BKE_font.h"
79 #include "BKE_global.h"
80 #include "BKE_main.h"
81 #include "BKE_mball.h"
82 #include "BKE_node.h"
83 #include "BKE_packedFile.h"
84 #include "BKE_texture.h"
85 #include "BKE_utildefines.h"
86 #include "BKE_pointcache.h"
87
88 #ifdef WITH_VERSE
89 #include "BKE_verse.h"
90 #endif
91
92 #include "BLI_vfontdata.h"
93
94 #include "BIF_fsmenu.h"
95 #include "BIF_gl.h"
96 #include "BIF_interface.h"
97 #include "BIF_usiblender.h"
98 #include "BIF_drawtext.h"
99 #include "BIF_editaction.h"
100 #include "BIF_editarmature.h"
101 #include "BIF_editlattice.h"
102 #include "BIF_editfont.h"
103 #include "BIF_editmesh.h"
104 #include "BIF_editmode_undo.h"
105 #include "BIF_editsound.h"
106 #include "BIF_filelist.h"
107 #include "BIF_poseobject.h"
108 #include "BIF_previewrender.h"
109 #include "BIF_renderwin.h"
110 #include "BIF_resources.h"
111 #include "BIF_screen.h"
112 #include "BIF_space.h"
113 #include "BIF_toolbox.h"
114 #include "BIF_cursors.h"
115
116 #ifdef WITH_VERSE
117 #include "BIF_verse.h"
118 #endif
119
120
121 #include "BSE_drawview.h"
122 #include "BSE_edit.h"
123 #include "BSE_editipo.h"
124 #include "BSE_filesel.h"
125 #include "BSE_headerbuttons.h"
126 #include "BSE_node.h"
127
128 #include "BLO_readfile.h"
129 #include "BLO_writefile.h"
130
131 #include "BDR_drawobject.h"
132 #include "BDR_editobject.h"
133 #include "BDR_editcurve.h"
134 #include "BDR_imagepaint.h"
135 #include "BDR_vpaint.h"
136
137 #include "BPY_extern.h"
138
139 #include "blendef.h"
140
141 #include "RE_pipeline.h"                /* RE_ free stuff */
142
143 #include "radio.h"
144 #include "datatoc.h"
145
146 #include "SYS_System.h"
147
148 #include "PIL_time.h"
149
150 /***/
151
152 /* define for setting colors in theme below */
153 #define SETCOL(col, r, g, b, a)  col[0]=r; col[1]=g; col[2]= b; col[3]= a;
154
155 /* patching UserDef struct, set globals for UI stuff */
156 static void init_userdef_file(void)
157 {
158         
159         BIF_InitTheme();        // sets default again
160         
161         mainwindow_set_filename_to_title("");   // empty string re-initializes title to "Blender"
162         countall();
163         G.save_over = 0;        // start with save preference untitled.blend
164         
165         /*  disable autoplay in .B.blend... */
166         G.fileflags &= ~G_FILE_AUTOPLAY;
167         
168         /* the UserDef struct is not corrected with do_versions() .... ugh! */
169         if(U.wheellinescroll == 0) U.wheellinescroll = 3;
170         if(U.menuthreshold1==0) {
171                 U.menuthreshold1= 5;
172                 U.menuthreshold2= 2;
173         }
174         if(U.tb_leftmouse==0) {
175                 U.tb_leftmouse= 5;
176                 U.tb_rightmouse= 5;
177         }
178         if(U.mixbufsize==0) U.mixbufsize= 2048;
179         if (BLI_streq(U.tempdir, "/")) {
180                 BLI_where_is_temp(U.tempdir, 0);
181         }
182         if (U.savetime <= 0) {
183                 U.savetime = 1;
184                 error(".B.blend is buggy, please consider removing it.\n");
185         }
186         /* transform widget settings */
187         if(U.tw_hotspot==0) {
188                 U.tw_hotspot= 14;
189                 U.tw_size= 20;                  // percentage of window size
190                 U.tw_handlesize= 16;    // percentage of widget radius
191         }
192         if(U.pad_rot_angle==0)
193                 U.pad_rot_angle= 15;
194         
195    if (U.ndof_pan==0) {
196         U.ndof_pan = 100;
197    }
198     if (U.ndof_rotate==0) {
199         U.ndof_rotate = 100;
200    }
201
202         if(U.flag & USER_CUSTOM_RANGE) 
203                 vDM_ColorBand_store(&U.coba_weight); /* signal for derivedmesh to use colorband */
204         
205         /* Auto-keyframing settings */
206         if(U.autokey_mode == 0) {
207                 /* AUTOKEY_MODE_NORMAL - AUTOKEY_ON = x  <==> 3 - 1 = 2 */
208                 U.autokey_mode |= 2;
209                 
210                 if(U.flag & (1<<15)) U.autokey_flag |= AUTOKEY_FLAG_INSERTAVAIL;
211                 if(U.flag & (1<<19)) U.autokey_flag |= AUTOKEY_FLAG_INSERTNEEDED;
212                 if(G.flags & (1<<30)) U.autokey_flag |= AUTOKEY_FLAG_AUTOMATKEY;
213         }
214         
215         if (G.main->versionfile <= 191) {
216                 strcpy(U.plugtexdir, U.textudir);
217                 strcpy(U.sounddir, "/");
218         }
219         
220         /* patch to set Dupli Armature */
221         if (G.main->versionfile < 220) {
222                 U.dupflag |= USER_DUP_ARM;
223         }
224         
225         /* userdef new option */
226         if (G.main->versionfile <= 222) {
227                 U.vrmlflag= USER_VRML_LAYERS;
228         }
229         
230         /* added seam, normal color, undo */
231         if (G.main->versionfile <= 234) {
232                 bTheme *btheme;
233                 
234                 U.uiflag |= USER_GLOBALUNDO;
235                 if (U.undosteps==0) U.undosteps=32;
236                 
237                 for(btheme= U.themes.first; btheme; btheme= btheme->next) {
238                         /* check for alpha==0 is safe, then color was never set */
239                         if(btheme->tv3d.edge_seam[3]==0) {
240                                 SETCOL(btheme->tv3d.edge_seam, 230, 150, 50, 255);
241                         }
242                         if(btheme->tv3d.normal[3]==0) {
243                                 SETCOL(btheme->tv3d.normal, 0x22, 0xDD, 0xDD, 255);
244                         }
245                         if(btheme->tv3d.face_dot[3]==0) {
246                                 SETCOL(btheme->tv3d.face_dot, 255, 138, 48, 255);
247                                 btheme->tv3d.facedot_size= 4;
248                         }
249                 }
250         }
251         if (G.main->versionfile <= 235) {
252                 /* illegal combo... */
253                 if (U.flag & USER_LMOUSESELECT) 
254                         U.flag &= ~USER_TWOBUTTONMOUSE;
255         }
256         if (G.main->versionfile <= 236) {
257                 bTheme *btheme;
258                 /* new space type */
259                 for(btheme= U.themes.first; btheme; btheme= btheme->next) {
260                         /* check for alpha==0 is safe, then color was never set */
261                         if(btheme->ttime.back[3]==0) {
262                                 btheme->ttime = btheme->tsnd;   // copy from sound
263                         }
264                         if(btheme->text.syntaxn[3]==0) {
265                                 SETCOL(btheme->text.syntaxn,    0, 0, 200, 255);        /* Numbers  Blue*/
266                                 SETCOL(btheme->text.syntaxl,    100, 0, 0, 255);        /* Strings  red */
267                                 SETCOL(btheme->text.syntaxc,    0, 100, 50, 255);       /* Comments greenish */
268                                 SETCOL(btheme->text.syntaxv,    95, 95, 0, 255);        /* Special */
269                                 SETCOL(btheme->text.syntaxb,    128, 0, 80, 255);       /* Builtin, red-purple */
270                         }
271                 }
272         }
273         if (G.main->versionfile <= 237) {
274                 bTheme *btheme;
275                 /* bone colors */
276                 for(btheme= U.themes.first; btheme; btheme= btheme->next) {
277                         /* check for alpha==0 is safe, then color was never set */
278                         if(btheme->tv3d.bone_solid[3]==0) {
279                                 SETCOL(btheme->tv3d.bone_solid, 200, 200, 200, 255);
280                                 SETCOL(btheme->tv3d.bone_pose, 80, 200, 255, 80);
281                         }
282                 }
283         }
284         if (G.main->versionfile <= 238) {
285                 bTheme *btheme;
286                 /* bone colors */
287                 for(btheme= U.themes.first; btheme; btheme= btheme->next) {
288                         /* check for alpha==0 is safe, then color was never set */
289                         if(btheme->tnla.strip[3]==0) {
290                                 SETCOL(btheme->tnla.strip_select,       0xff, 0xff, 0xaa, 255);
291                                 SETCOL(btheme->tnla.strip, 0xe4, 0x9c, 0xc6, 255);
292                         }
293                 }
294         }
295         if (G.main->versionfile <= 239) {
296                 bTheme *btheme;
297                 
298                 for(btheme= U.themes.first; btheme; btheme= btheme->next) {
299                         /* Lamp theme, check for alpha==0 is safe, then color was never set */
300                         if(btheme->tv3d.lamp[3]==0) {
301                                 SETCOL(btheme->tv3d.lamp,       0, 0, 0, 40);
302 /* TEMPORAL, remove me! (ton) */                                
303                                 U.uiflag |= USER_PLAINMENUS;
304                         }
305                         
306                         /* check for text field selection highlight, set it to text editor highlight by default */
307                         if(btheme->tui.textfield_hi[3]==0) {
308                                 SETCOL(btheme->tui.textfield_hi,        
309                                         btheme->text.shade2[0], 
310                                         btheme->text.shade2[1], 
311                                         btheme->text.shade2[2],
312                                         255);
313                         }
314                 }
315                 if(U.obcenter_dia==0) U.obcenter_dia= 6;
316         }
317         if (G.main->versionfile <= 241) {
318                 bTheme *btheme;
319                 for(btheme= U.themes.first; btheme; btheme= btheme->next) {
320                         /* Node editor theme, check for alpha==0 is safe, then color was never set */
321                         if(btheme->tnode.syntaxn[3]==0) {
322                                 /* re-uses syntax color storage */
323                                 btheme->tnode= btheme->tv3d;
324                                 SETCOL(btheme->tnode.edge_select, 255, 255, 255, 255);
325                                 SETCOL(btheme->tnode.syntaxl, 150, 150, 150, 255);      /* TH_NODE, backdrop */
326                                 SETCOL(btheme->tnode.syntaxn, 129, 131, 144, 255);      /* in/output */
327                                 SETCOL(btheme->tnode.syntaxb, 127,127,127, 255);        /* operator */
328                                 SETCOL(btheme->tnode.syntaxv, 142, 138, 145, 255);      /* generator */
329                                 SETCOL(btheme->tnode.syntaxc, 120, 145, 120, 255);      /* group */
330                         }
331                         /* Group theme colors */
332                         if(btheme->tv3d.group[3]==0) {
333                                 SETCOL(btheme->tv3d.group, 0x10, 0x40, 0x10, 255);
334                                 SETCOL(btheme->tv3d.group_active, 0x66, 0xFF, 0x66, 255);
335                         }
336                         /* Sequence editor theme*/
337                         if(btheme->tseq.movie[3]==0) {
338                                 SETCOL(btheme->tseq.movie,      81, 105, 135, 255);
339                                 SETCOL(btheme->tseq.image,      109, 88, 129, 255);
340                                 SETCOL(btheme->tseq.scene,      78, 152, 62, 255);
341                                 SETCOL(btheme->tseq.audio,      46, 143, 143, 255);
342                                 SETCOL(btheme->tseq.effect,     169, 84, 124, 255);
343                                 SETCOL(btheme->tseq.plugin,     126, 126, 80, 255);
344                                 SETCOL(btheme->tseq.transition, 162, 95, 111, 255);
345                                 SETCOL(btheme->tseq.meta,       109, 145, 131, 255);
346                         }
347                         if(!(btheme->tui.iconfile)) {
348                                 BLI_strncpy(btheme->tui.iconfile, "", sizeof(btheme->tui.iconfile));
349                         }
350                 }
351                 
352                 /* set defaults for 3D View rotating axis indicator */ 
353                 /* since size can't be set to 0, this indicates it's not saved in .B.blend */
354                 if (U.rvisize == 0) {
355                         U.rvisize = 15;
356                         U.rvibright = 8;
357                         U.uiflag |= USER_SHOW_ROTVIEWICON;
358                 }
359                 
360         }
361         if (G.main->versionfile <= 242) {
362                 bTheme *btheme;
363                 
364                 for(btheme= U.themes.first; btheme; btheme= btheme->next) {
365                         /* long keyframe color */
366                         /* check for alpha==0 is safe, then color was never set */
367                         if(btheme->tact.strip[3]==0) {
368                                 SETCOL(btheme->tv3d.edge_sharp, 255, 32, 32, 255);
369                                 SETCOL(btheme->tact.strip_select,       0xff, 0xff, 0xaa, 204);
370                                 SETCOL(btheme->tact.strip, 0xe4, 0x9c, 0xc6, 204);
371                         }
372                         
373                         /* IPO-Editor - Vertex Size*/
374                         if(btheme->tipo.vertex_size == 0) {
375                                 btheme->tipo.vertex_size= 3;
376                         }
377                 }
378         }
379         if (G.main->versionfile <= 243) {
380                 /* set default number of recently-used files (if not set) */
381                 if (U.recent_files == 0) U.recent_files = 10;
382         }
383         if (G.main->versionfile < 245 || (G.main->versionfile == 245 && G.main->subversionfile < 3)) {
384                 bTheme *btheme;
385                 for(btheme= U.themes.first; btheme; btheme= btheme->next) {
386                         SETCOL(btheme->tv3d.editmesh_active, 255, 255, 255, 128);
387                 }
388                 if(U.coba_weight.tot==0)
389                         init_colorband(&U.coba_weight, 1);
390         }
391         if ((G.main->versionfile < 245) || (G.main->versionfile == 245 && G.main->subversionfile < 11)) {
392                 bTheme *btheme;
393                 for (btheme= U.themes.first; btheme; btheme= btheme->next) {
394                         /* these should all use the same color */
395                         SETCOL(btheme->tv3d.cframe, 0x60, 0xc0, 0x40, 255);
396                         SETCOL(btheme->tipo.cframe, 0x60, 0xc0, 0x40, 255);
397                         SETCOL(btheme->tact.cframe, 0x60, 0xc0, 0x40, 255);
398                         SETCOL(btheme->tnla.cframe, 0x60, 0xc0, 0x40, 255);
399                         SETCOL(btheme->tseq.cframe, 0x60, 0xc0, 0x40, 255);
400                         SETCOL(btheme->tsnd.cframe, 0x60, 0xc0, 0x40, 255);
401                         SETCOL(btheme->ttime.cframe, 0x60, 0xc0, 0x40, 255);
402                 }
403         }
404         if ((G.main->versionfile < 245) || (G.main->versionfile == 245 && G.main->subversionfile < 13)) {
405                 bTheme *btheme;
406                 for (btheme= U.themes.first; btheme; btheme= btheme->next) {
407                         /* action channel groups (recolor anyway) */
408                         SETCOL(btheme->tact.group, 0x39, 0x7d, 0x1b, 255);
409                         SETCOL(btheme->tact.group_active, 0x7d, 0xe9, 0x60, 255);
410                         
411                         /* bone custom-color sets */
412                         // FIXME: this check for initialised colors is bad
413                         if (btheme->tarm[0].solid[3] == 0) {
414                                         /* set 1 */
415                                 SETCOL(btheme->tarm[0].solid, 0x9a, 0x00, 0x00, 255);
416                                 SETCOL(btheme->tarm[0].select, 0xbd, 0x11, 0x11, 255);
417                                 SETCOL(btheme->tarm[0].active, 0xf7, 0x0a, 0x0a, 255);
418                                         /* set 2 */
419                                 SETCOL(btheme->tarm[1].solid, 0xf7, 0x40, 0x18, 255);
420                                 SETCOL(btheme->tarm[1].select, 0xf6, 0x69, 0x13, 255);
421                                 SETCOL(btheme->tarm[1].active, 0xfa, 0x99, 0x00, 255);
422                                         /* set 3 */
423                                 SETCOL(btheme->tarm[2].solid, 0x1e, 0x91, 0x09, 255);
424                                 SETCOL(btheme->tarm[2].select, 0x59, 0xb7, 0x0b, 255);
425                                 SETCOL(btheme->tarm[2].active, 0x83, 0xef, 0x1d, 255);
426                                         /* set 4 */
427                                 SETCOL(btheme->tarm[3].solid, 0x0a, 0x36, 0x94, 255);
428                                 SETCOL(btheme->tarm[3].select, 0x36, 0x67, 0xdf, 255);
429                                 SETCOL(btheme->tarm[3].active, 0x5e, 0xc1, 0xef, 255);
430                                         /* set 5 */
431                                 SETCOL(btheme->tarm[4].solid, 0xa9, 0x29, 0x4e, 255);
432                                 SETCOL(btheme->tarm[4].select, 0xc1, 0x41, 0x6a, 255);
433                                 SETCOL(btheme->tarm[4].active, 0xf0, 0x5d, 0x91, 255);
434                                         /* set 6 */
435                                 SETCOL(btheme->tarm[5].solid, 0x43, 0x0c, 0x78, 255);
436                                 SETCOL(btheme->tarm[5].select, 0x54, 0x3a, 0xa3, 255);
437                                 SETCOL(btheme->tarm[5].active, 0x87, 0x64, 0xd5, 255);
438                                         /* set 7 */
439                                 SETCOL(btheme->tarm[6].solid, 0x24, 0x78, 0x5a, 255);
440                                 SETCOL(btheme->tarm[6].select, 0x3c, 0x95, 0x79, 255);
441                                 SETCOL(btheme->tarm[6].active, 0x6f, 0xb6, 0xab, 255);
442                                         /* set 8 */
443                                 SETCOL(btheme->tarm[7].solid, 0x4b, 0x70, 0x7c, 255);
444                                 SETCOL(btheme->tarm[7].select, 0x6a, 0x86, 0x91, 255);
445                                 SETCOL(btheme->tarm[7].active, 0x9b, 0xc2, 0xcd, 255);
446                                         /* set 9 */
447                                 SETCOL(btheme->tarm[8].solid, 0xf4, 0xc9, 0x0c, 255);
448                                 SETCOL(btheme->tarm[8].select, 0xee, 0xc2, 0x36, 255);
449                                 SETCOL(btheme->tarm[8].active, 0xf3, 0xff, 0x00, 255);
450                                         /* set 10 */
451                                 SETCOL(btheme->tarm[9].solid, 0x1e, 0x20, 0x24, 255);
452                                 SETCOL(btheme->tarm[9].select, 0x48, 0x4c, 0x56, 255);
453                                 SETCOL(btheme->tarm[9].active, 0xff, 0xff, 0xff, 255);
454                                         /* set 11 */
455                                 SETCOL(btheme->tarm[10].solid, 0x6f, 0x2f, 0x6a, 255);
456                                 SETCOL(btheme->tarm[10].select, 0x98, 0x45, 0xbe, 255);
457                                 SETCOL(btheme->tarm[10].active, 0xd3, 0x30, 0xd6, 255);
458                                         /* set 12 */
459                                 SETCOL(btheme->tarm[11].solid, 0x6c, 0x8e, 0x22, 255);
460                                 SETCOL(btheme->tarm[11].select, 0x7f, 0xb0, 0x22, 255);
461                                 SETCOL(btheme->tarm[11].active, 0xbb, 0xef, 0x5b, 255);
462                                         /* set 13 */
463                                 SETCOL(btheme->tarm[12].solid, 0x8d, 0x8d, 0x8d, 255);
464                                 SETCOL(btheme->tarm[12].select, 0xb0, 0xb0, 0xb0, 255);
465                                 SETCOL(btheme->tarm[12].active, 0xde, 0xde, 0xde, 255);
466                                         /* set 14 */
467                                 SETCOL(btheme->tarm[13].solid, 0x83, 0x43, 0x26, 255);
468                                 SETCOL(btheme->tarm[13].select, 0x8b, 0x58, 0x11, 255);
469                                 SETCOL(btheme->tarm[13].active, 0xbd, 0x6a, 0x11, 255);
470                                         /* set 15 */
471                                 SETCOL(btheme->tarm[14].solid, 0x08, 0x31, 0x0e, 255);
472                                 SETCOL(btheme->tarm[14].select, 0x1c, 0x43, 0x0b, 255);
473                                 SETCOL(btheme->tarm[14].active, 0x34, 0x62, 0x2b, 255);
474                         }
475                 }
476         }
477         if ((G.main->versionfile < 245) || (G.main->versionfile == 245 && G.main->subversionfile < 16)) {
478                 U.flag |= USER_ADD_VIEWALIGNED|USER_ADD_EDITMODE;
479         }
480         
481         /* GL Texture Garbage Collection (variable abused above!) */
482         if (U.textimeout == 0) {
483                 U.texcollectrate = 60;
484                 U.textimeout = 120;
485         }
486         if (U.memcachelimit <= 0) {
487                 U.memcachelimit = 32;
488         }
489         if (U.frameserverport == 0) {
490                 U.frameserverport = 8080;
491         }
492
493         MEM_CacheLimiter_set_maximum(U.memcachelimit * 1024 * 1024);
494         
495         reset_autosave();
496         
497 #ifdef INTERNATIONAL
498         read_languagefile();
499 #endif
500         
501         refresh_interface_font();
502
503 #ifdef WITH_VERSE
504         if(strlen(U.versemaster)<1) {
505                         strcpy(U.versemaster, "master.uni-verse.org");
506         }
507         if(strlen(U.verseuser)<1) {
508                         char *name = verse_client_name();
509                         strcpy(U.verseuser, name);
510                         MEM_freeN(name);
511         }
512 #endif
513
514 }
515
516 #ifdef WITH_VERSE
517 extern ListBase session_list;
518 #endif
519
520 void BIF_read_file(char *name)
521 {
522         extern short winqueue_break; /* editscreen.c */
523         int retval;
524 #ifdef WITH_VERSE
525         struct VerseSession *session;
526         struct VNode *vnode;
527
528         session = session_list.first;
529         while(session) {
530                 vnode = session->nodes.lb.first;
531                 while(vnode) {
532                         switch(vnode->type) {
533                                 case V_NT_OBJECT:
534                                         unsubscribe_from_obj_node(vnode);
535                                         break;
536                                 case V_NT_GEOMETRY:
537                                         unsubscribe_from_geom_node(vnode);
538                                         break;
539                                 case V_NT_BITMAP:
540                                         unsubscribe_from_bitmap_node(vnode);
541                                         break;
542                         }
543                         vnode = vnode->next;
544                 }
545                 session = session->next;
546         }
547 #endif
548
549         /* first try to read exotic file formats... */
550         /* it throws error box when file doesnt exist and returns -1 */
551         retval= BKE_read_exotic(name);
552         
553         if (retval== 0) {
554                 BIF_clear_tempfiles();
555                 
556                 /* we didn't succeed, now try to read Blender file */
557                 retval= BKE_read_file(name, NULL);
558
559                 mainwindow_set_filename_to_title(G.main->name);
560                 countall();
561                 sound_initialize_sounds();
562
563                 winqueue_break= 1;      /* leave queues everywhere */
564
565                 if(retval==2) init_userdef_file();      // in case a userdef is read from regular .blend
566                 
567                 if (retval!=0) G.relbase_valid = 1;
568
569                 undo_editmode_clear();
570                 BKE_reset_undo();
571                 BKE_write_undo("original");     /* save current state */
572
573                 refresh_interface_font();
574         }
575         else if(retval==1)
576                 BIF_undo_push("Import file");
577 }
578
579 static void outliner_242_patch(void)
580 {
581         ScrArea *sa;
582         
583         for(sa= G.curscreen->areabase.first; sa; sa= sa->next) {
584                 SpaceLink *sl= sa->spacedata.first;
585                 for(; sl; sl= sl->next) {
586                         if(sl->spacetype==SPACE_OOPS) {
587                                 SpaceOops *soops= (SpaceOops *)sl;
588                                 if(soops->type!=SO_OUTLINER) {
589                                         soops->type= SO_OUTLINER;
590                                         init_v2d_oops(sa, soops);
591                                 }
592                         }
593                 }
594         }
595         G.fileflags |= G_FILE_GAME_MAT;
596 }
597
598 /* only here settings for fullscreen */
599 int BIF_read_homefile(int from_memory)
600 {
601         char tstr[FILE_MAXDIR+FILE_MAXFILE], scestr[FILE_MAX];
602         char *home= BLI_gethome();
603         int success;
604         struct TmpFont *tf;
605         
606         BIF_clear_tempfiles();
607         
608         BLI_clean(home);
609
610         tf= G.ttfdata.first;
611         while(tf)
612         {
613                 freePackedFile(tf->pf);
614                 tf->pf = NULL;
615                 tf->vfont = NULL;
616                 tf= tf->next;
617         }
618         BLI_freelistN(&G.ttfdata);
619                 
620         G.relbase_valid = 0;
621         if (!from_memory) BLI_make_file_string(G.sce, tstr, home, ".B.blend");
622         BLI_strncpy(scestr, G.sce, FILE_MAX);   /* temporal store */
623         
624         /* prevent loading no UI */
625         G.fileflags &= ~G_FILE_NO_UI;
626         
627         if (!from_memory && BLI_exists(tstr)) {
628                 success = BKE_read_file(tstr, NULL);
629         } else {
630                 success = BKE_read_file_from_memory(datatoc_B_blend, datatoc_B_blend_size, NULL);
631                 /* outliner patch for 2.42 .b.blend */
632                 outliner_242_patch();
633         }
634
635         BLI_clean(scestr);
636         strcpy(G.sce, scestr);
637
638         space_set_commmandline_options();
639         
640         init_userdef_file();
641
642         undo_editmode_clear();
643         BKE_reset_undo();
644         BKE_write_undo("original");     /* save current state */
645         
646         /* if from memory, need to refresh python scripts */
647         if (from_memory) {
648                 BPY_path_update();
649         }
650         return success;
651 }
652
653
654 static void get_autosave_location(char buf[FILE_MAXDIR+FILE_MAXFILE])
655 {
656         char pidstr[32];
657 #ifdef WIN32
658         char subdir[9];
659         char savedir[FILE_MAXDIR];
660 #endif
661
662         sprintf(pidstr, "%d.blend", abs(getpid()));
663         
664 #ifdef WIN32
665         if (!BLI_exists(btempdir)) {
666                 BLI_strncpy(subdir, "autosave", sizeof(subdir));
667                 BLI_make_file_string("/", savedir, BLI_gethome(), subdir);
668                 
669                 /* create a new autosave dir
670                  * function already checks for existence or not */
671                 BLI_recurdir_fileops(savedir);
672         
673                 BLI_make_file_string("/", buf, savedir, pidstr);
674                 return;
675         }
676 #endif
677         
678         BLI_make_file_string("/", buf, btempdir, pidstr);
679 }
680
681 void BIF_read_autosavefile(void)
682 {
683         char tstr[FILE_MAX], scestr[FILE_MAX];
684         int save_over;
685
686         BLI_strncpy(scestr, G.sce, FILE_MAX);   /* temporal store */
687         
688         get_autosave_location(tstr);
689
690         save_over = G.save_over;
691         BKE_read_file(tstr, NULL);
692         G.save_over = save_over;
693         BLI_strncpy(G.sce, scestr, FILE_MAX);
694 }
695
696 /* free strings of open recent files */
697 static void free_openrecent(void)
698 {
699         struct RecentFile *recent;
700
701         for(recent = G.recent_files.first; recent; recent=recent->next)
702                 MEM_freeN(recent->filename);
703
704         BLI_freelistN(&(G.recent_files));
705 }
706
707 static void readBlog(void)
708 {
709         char name[FILE_MAX], filename[FILE_MAX];
710         LinkNode *l, *lines;
711         struct RecentFile *recent;
712         char *line;
713         int num;
714
715         BLI_make_file_string("/", name, BLI_gethome(), ".Blog");
716         lines= BLI_read_file_as_lines(name);
717
718         G.recent_files.first = G.recent_files.last = NULL;
719
720         /* read list of recent opend files from .Blog to memory */
721         for (l= lines, num= 0; l && (num<U.recent_files); l= l->next, num++) {
722                 line = l->link;
723                 if (!BLI_streq(line, "")) {
724                         if (num==0) 
725                                 strcpy(G.sce, line);
726                         
727                         recent = (RecentFile*)MEM_mallocN(sizeof(RecentFile),"RecentFile");
728                         BLI_addtail(&(G.recent_files), recent);
729                         recent->filename = (char*)MEM_mallocN(sizeof(char)*(strlen(line)+1), "name of file");
730                         recent->filename[0] = '\0';
731                         
732                         strcpy(recent->filename, line);
733                 }
734         }
735
736         if(G.sce[0] == 0)
737                 BLI_make_file_string("/", G.sce, BLI_gethome(), "untitled.blend");
738         
739         BLI_free_file_lines(lines);
740
741 #ifdef WIN32
742         /* Add the drive names to the listing */
743         {
744                 __int64 tmp;
745                 char folder[MAX_PATH];
746                 char tmps[4];
747                 int i;
748                         
749                 tmp= GetLogicalDrives();
750                 
751                 for (i=2; i < 26; i++) {
752                         if ((tmp>>i) & 1) {
753                                 tmps[0]='a'+i;
754                                 tmps[1]=':';
755                                 tmps[2]='\\';
756                                 tmps[3]=0;
757                                 
758                                 fsmenu_insert_entry(tmps, 0, 0);
759                         }
760                 }
761
762                 /* Adding Desktop and My Documents */
763                 fsmenu_append_separator();
764
765                 SHGetSpecialFolderPath(0, folder, CSIDL_PERSONAL, 0);
766                 fsmenu_insert_entry(folder, 0, 0);
767                 SHGetSpecialFolderPath(0, folder, CSIDL_DESKTOPDIRECTORY, 0);
768                 fsmenu_insert_entry(folder, 0, 0);
769
770                 fsmenu_append_separator();
771         }
772 #else
773         /* add home dir on linux systems */
774         fsmenu_insert_entry(BLI_gethome(), 0, 0);
775 #endif
776
777         BLI_make_file_string(G.sce, name, BLI_gethome(), ".Bfs");
778         lines= BLI_read_file_as_lines(name);
779
780         for (l= lines; l; l= l->next) {
781                 char *line= l->link;
782                         
783                 if (!BLI_streq(line, "")) {
784                         fsmenu_insert_entry(line, 0, 1);
785                 }
786         }
787
788         fsmenu_append_separator();
789         
790         /* add last saved file */
791         BLI_split_dirfile_basic(G.sce, name, filename); /* G.sce shouldn't be relative */
792         
793         fsmenu_insert_entry(name, 0, 0);
794         
795         BLI_free_file_lines(lines);
796 }
797
798 static void writeBlog(void)
799 {
800         struct RecentFile *recent, *next_recent;
801         char name[FILE_MAXDIR+FILE_MAXFILE];
802         FILE *fp;
803         int i;
804
805         BLI_make_file_string("/", name, BLI_gethome(), ".Blog");
806
807         recent = G.recent_files.first;
808         /* refresh .Blog of recent opened files, when current file was changed */
809         if(!(recent) || (strcmp(recent->filename, G.sce)!=0)) {
810                 fp= fopen(name, "w");
811                 if (fp) {
812                         /* add current file to the beginning of list */
813                         recent = (RecentFile*)MEM_mallocN(sizeof(RecentFile),"RecentFile");
814                         recent->filename = (char*)MEM_mallocN(sizeof(char)*(strlen(G.sce)+1), "name of file");
815                         recent->filename[0] = '\0';
816                         strcpy(recent->filename, G.sce);
817                         BLI_addhead(&(G.recent_files), recent);
818                         /* write current file to .Blog */
819                         fprintf(fp, "%s\n", recent->filename);
820                         recent = recent->next;
821                         i=1;
822                         /* write rest of recent opened files to .Blog */
823                         while((i<U.recent_files) && (recent)){
824                                 /* this prevents to have duplicities in list */
825                                 if (strcmp(recent->filename, G.sce)!=0) {
826                                         fprintf(fp, "%s\n", recent->filename);
827                                         recent = recent->next;
828                                 }
829                                 else {
830                                         next_recent = recent->next;
831                                         MEM_freeN(recent->filename);
832                                         BLI_freelinkN(&(G.recent_files), recent);
833                                         recent = next_recent;
834                                 }
835                                 i++;
836                         }
837                         fclose(fp);
838                 }
839         }
840 }
841
842 static void do_history(char *name)
843 {
844         char tempname1[FILE_MAXDIR+FILE_MAXFILE], tempname2[FILE_MAXDIR+FILE_MAXFILE];
845         int hisnr= U.versions;
846         
847         if(U.versions==0) return;
848         if(strlen(name)<2) return;
849                 
850         while(  hisnr > 1) {
851                 sprintf(tempname1, "%s%d", name, hisnr-1);
852                 sprintf(tempname2, "%s%d", name, hisnr);
853         
854                 if(BLI_rename(tempname1, tempname2))
855                         error("Unable to make version backup");
856                         
857                 hisnr--;
858         }
859                 
860         /* is needed when hisnr==1 */
861         sprintf(tempname1, "%s%d", name, hisnr);
862         
863         if(BLI_rename(name, tempname1))
864                 error("Unable to make version backup");
865 }
866
867 void BIF_write_file(char *target)
868 {
869         Library *li;
870         int writeflags, len;
871         char di[FILE_MAX];
872         char *err;
873         
874         len = strlen(target);
875         
876         if (len == 0) return;
877         if (len >= FILE_MAX) {
878                 error("Path too long, cannot save");
879                 return;
880         }
881  
882         /* send the OnSave event */
883         if (G.f & G_DOSCRIPTLINKS) BPY_do_pyscript(&G.scene->id, SCRIPT_ONSAVE);
884
885         for (li= G.main->library.first; li; li= li->id.next) {
886                 if (BLI_streq(li->name, target)) {
887                         error("Cannot overwrite used library");
888                         return;
889                 }
890         }
891         
892         if (!BLO_has_bfile_extension(target) && (len+6 < FILE_MAX)) {
893                 sprintf(di, "%s.blend", target);
894         } else {
895                 strcpy(di, target);
896         }
897
898         if (BLI_exists(di)) {
899                 if(!saveover(di))
900                         return; 
901         }
902         
903         if(G.obedit) {
904                 exit_editmode(0);       /* 0 = no free data */
905         }
906         if (G.fileflags & G_AUTOPACK) {
907                 packAll();
908         }
909         
910         waitcursor(1);  // exit_editmode sets cursor too
911
912         do_history(di);
913         
914         /* we use the UserDef to define compression flag */
915         writeflags= G.fileflags & ~G_FILE_COMPRESS;
916         if(U.flag & USER_FILECOMPRESS)
917                 writeflags |= G_FILE_COMPRESS;
918         
919         if (BLO_write_file(di, writeflags, &err)) {
920                 strcpy(G.sce, di);
921                 G.relbase_valid = 1;
922                 BLI_strncpy(G.main->name, di, FILE_MAX);        /* is guaranteed current file */
923
924                 mainwindow_set_filename_to_title(G.main->name);
925
926                 G.save_over = 1;
927
928                 writeBlog();
929         } else {
930                 error("failed to write blend file: %s", err);
931         }
932
933         waitcursor(0);
934 }
935
936 void BIF_write_homefile(void)
937 {
938         char *err, tstr[FILE_MAXDIR+FILE_MAXFILE];
939         int write_flags;
940         
941         BLI_make_file_string("/", tstr, BLI_gethome(), ".B.blend");
942                 
943         /*  force save as regular blend file */
944         write_flags = G.fileflags & ~(G_FILE_COMPRESS | G_FILE_LOCK | G_FILE_SIGN);
945         
946         if (!BLO_write_file(tstr, write_flags, &err)) {
947                 error("failed writing defaults: %s", err);
948         }
949 }
950
951 void BIF_write_autosave(void)
952 {
953         char *err, tstr[FILE_MAXDIR+FILE_MAXFILE];
954         int write_flags;
955         
956         get_autosave_location(tstr);
957
958                 /*  force save as regular blend file */
959         write_flags = G.fileflags & ~(G_FILE_COMPRESS | G_FILE_LOCK | G_FILE_SIGN);
960         if (!BLO_write_file(tstr, write_flags, &err)) {
961                 fprintf(stderr, "failed to write autosave: %s\n", err); /* using error(...) is too annoying here */
962         }
963 }
964
965 /* remove temp files assosiated with this blend file when quitting, loading or saving in a new path */
966 void BIF_clear_tempfiles( void )
967 {
968         /* TODO - remove exr files from the temp dir */
969         
970         if (!G.relbase_valid) { /* We could have pointcache saved in tyhe temp dir, if its there */
971                 BKE_ptcache_remove();
972         }
973 }
974
975 /* if global undo; remove tempsave, otherwise rename */
976 static void delete_autosave(void)
977 {
978         char tstr[FILE_MAXDIR+FILE_MAXFILE];
979         
980         get_autosave_location(tstr);
981
982         if (BLI_exists(tstr)) {
983                 char str[FILE_MAXDIR+FILE_MAXFILE];
984                 BLI_make_file_string("/", str, btempdir, "quit.blend");
985
986                 if(U.uiflag & USER_GLOBALUNDO) BLI_delete(tstr, 0, 0);
987                 else BLI_rename(tstr, str);
988         }
989 }
990
991 /***/
992
993 static void initbuttons(void)
994 {
995         uiDefFont(UI_HELVB, 
996                                 BMF_GetFont(BMF_kHelveticaBold14), 
997                                 BMF_GetFont(BMF_kHelveticaBold12), 
998                                 BMF_GetFont(BMF_kHelveticaBold10), 
999                                 BMF_GetFont(BMF_kHelveticaBold8));
1000         uiDefFont(UI_HELV, 
1001                                 BMF_GetFont(BMF_kHelvetica12), 
1002                                 BMF_GetFont(BMF_kHelvetica12), 
1003                                 BMF_GetFont(BMF_kHelvetica10), 
1004                                 BMF_GetFont(BMF_kHelveticaBold8));
1005
1006         glClearColor(.7f, .7f, .6f, 0.0);
1007         
1008         G.font= BMF_GetFont(BMF_kHelvetica12);
1009         G.fonts= BMF_GetFont(BMF_kHelvetica10);
1010         G.fontss= BMF_GetFont(BMF_kHelveticaBold8);
1011
1012         clear_matcopybuf();
1013         
1014         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1015 }
1016
1017
1018 static void sound_init_listener(void)
1019 {
1020         G.listener = MEM_callocN(sizeof(bSoundListener), "soundlistener");
1021         G.listener->gain = 1.0;
1022         G.listener->dopplerfactor = 1.0;
1023         G.listener->dopplervelocity = 340.29f;
1024 }
1025
1026 void BIF_init(void)
1027 {
1028
1029         initscreen();   /* for (visuele) speed, this first, then setscreen */
1030         initbuttons();
1031         InitCursorData();
1032         sound_init_listener();
1033         init_node_butfuncs();
1034         
1035         BIF_preview_init_dbase();
1036         BIF_read_homefile(0);
1037
1038         BIF_resources_init();   /* after homefile, to dynamically load an icon file based on theme settings */
1039         
1040         BIF_filelist_init_icons();
1041
1042         init_gl_stuff();        /* drawview.c, after homefile */
1043         glewInit();
1044         readBlog();
1045         BLI_strncpy(G.lib, G.sce, FILE_MAX);
1046 }
1047
1048 /***/
1049
1050 extern ListBase editNurb;
1051 extern ListBase editelems;
1052
1053 void exit_usiblender(void)
1054 {
1055         struct TmpFont *tf;
1056         
1057         BIF_clear_tempfiles();
1058         
1059         tf= G.ttfdata.first;
1060         while(tf)
1061         {
1062                 freePackedFile(tf->pf);
1063                 tf->pf= NULL;
1064                 tf->vfont= NULL;
1065                 tf= tf->next;
1066         }
1067         BLI_freelistN(&G.ttfdata);
1068 #ifdef WITH_VERSE
1069         end_all_verse_sessions();
1070 #endif
1071         free_openrecent();
1072
1073         freeAllRad();
1074         BKE_freecubetable();
1075
1076         if (G.background == 0)
1077                 sound_end_all_sounds();
1078
1079         if(G.obedit) {
1080                 if(G.obedit->type==OB_FONT) {
1081                         free_editText();
1082                 }
1083                 else if(G.obedit->type==OB_MBALL) BLI_freelistN(&editelems);
1084                 free_editMesh(G.editMesh);
1085         }
1086
1087         free_editLatt();
1088         free_editArmature();
1089         free_posebuf();
1090
1091         /* before free_blender so py's gc happens while library still exists */
1092         /* needed at least for a rare sigsegv that can happen in pydrivers */
1093         BPY_end_python();
1094
1095         fastshade_free_render();        /* shaded view */
1096         free_blender();                         /* blender.c, does entire library */
1097         free_matcopybuf();
1098         free_ipocopybuf();
1099         free_actcopybuf();
1100         free_vertexpaint();
1101         free_imagepaint();
1102         
1103         /* editnurb can remain to exist outside editmode */
1104         freeNurblist(&editNurb);
1105
1106         fsmenu_free();
1107
1108 #ifdef INTERNATIONAL
1109         free_languagemenu();
1110 #endif
1111         
1112         RE_FreeAllRender();
1113         
1114         free_txt_data();
1115
1116         sound_exit_audio();
1117         if(G.listener) MEM_freeN(G.listener);
1118
1119
1120         libtiff_exit();
1121
1122 #ifdef WITH_QUICKTIME
1123         quicktime_exit();
1124 #endif
1125
1126         /* undo free stuff */
1127         undo_editmode_clear();
1128         
1129         BKE_undo_save_quit();   // saves quit.blend if global undo is on
1130         BKE_reset_undo(); 
1131         
1132         if (!G.background) {
1133                 BIF_resources_free();
1134                 
1135                 BIF_filelist_free_icons();
1136
1137                 BIF_free_render_spare();
1138                 BIF_close_render_display();
1139                 mainwindow_close();
1140         }
1141
1142 #ifdef INTERNATIONAL
1143         FTF_End();
1144 #endif
1145
1146         if (copybuf) MEM_freeN(copybuf);
1147         if (copybufinfo) MEM_freeN(copybufinfo);
1148
1149 //      
1150         BLI_freelistN(&U.themes);
1151         BIF_preview_free_dbase();
1152         
1153         if(totblock!=0) {
1154                 printf("Error Totblock: %d\n",totblock);
1155                 MEM_printmemlist();
1156         }
1157         delete_autosave();
1158         
1159         printf("\nBlender quit\n");
1160
1161 #ifdef WIN32   
1162         /* ask user to press enter when in debug mode */
1163         if(G.f & G_DEBUG) {
1164                 printf("press enter key to exit...\n\n");
1165                 getchar();
1166         }
1167 #endif 
1168
1169
1170         SYS_DeleteSystem(SYS_GetSystem());
1171         
1172         exit(G.afbreek==1);
1173 }