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