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