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