added scons option BF_WITH_PYTHON (defined as DISABLE_PYTHON)
[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 #ifndef DISABLE_PYTHON
138 #include "BPY_extern.h"
139 #endif
140
141 #include "blendef.h"
142
143 #include "RE_pipeline.h"                /* RE_ free stuff */
144
145 #include "radio.h"
146 #include "datatoc.h"
147
148 #include "SYS_System.h"
149
150 #include "PIL_time.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 #ifndef DISABLE_PYTHON
684         /* if from memory, need to refresh python scripts */
685         if (from_memory) {
686                 BPY_path_update();
687         }
688 #endif
689         return success;
690 }
691
692
693 static void get_autosave_location(char buf[FILE_MAXDIR+FILE_MAXFILE])
694 {
695         char pidstr[32];
696 #ifdef WIN32
697         char subdir[9];
698         char savedir[FILE_MAXDIR];
699 #endif
700
701         sprintf(pidstr, "%d.blend", abs(getpid()));
702         
703 #ifdef WIN32
704         if (!BLI_exists(btempdir)) {
705                 BLI_strncpy(subdir, "autosave", sizeof(subdir));
706                 BLI_make_file_string("/", savedir, BLI_gethome(), subdir);
707                 
708                 /* create a new autosave dir
709                  * function already checks for existence or not */
710                 BLI_recurdir_fileops(savedir);
711         
712                 BLI_make_file_string("/", buf, savedir, pidstr);
713                 return;
714         }
715 #endif
716         
717         BLI_make_file_string("/", buf, btempdir, pidstr);
718 }
719
720 void BIF_read_autosavefile(void)
721 {
722         char tstr[FILE_MAX], scestr[FILE_MAX];
723         int save_over;
724
725         BLI_strncpy(scestr, G.sce, FILE_MAX);   /* temporal store */
726         
727         get_autosave_location(tstr);
728
729         save_over = G.save_over;
730         BKE_read_file(tstr, NULL);
731         G.save_over = save_over;
732         BLI_strncpy(G.sce, scestr, FILE_MAX);
733 }
734
735 /* free strings of open recent files */
736 static void free_openrecent(void)
737 {
738         struct RecentFile *recent;
739
740         for(recent = G.recent_files.first; recent; recent=recent->next)
741                 MEM_freeN(recent->filename);
742
743         BLI_freelistN(&(G.recent_files));
744 }
745
746 static void readBlog(void)
747 {
748         char name[FILE_MAX], filename[FILE_MAX];
749         LinkNode *l, *lines;
750         struct RecentFile *recent;
751         char *line;
752         int num;
753
754         BLI_make_file_string("/", name, BLI_gethome(), ".Blog");
755         lines= BLI_read_file_as_lines(name);
756
757         G.recent_files.first = G.recent_files.last = NULL;
758
759         /* read list of recent opend files from .Blog to memory */
760         for (l= lines, num= 0; l && (num<U.recent_files); l= l->next, num++) {
761                 line = l->link;
762                 if (!BLI_streq(line, "")) {
763                         if (num==0) 
764                                 strcpy(G.sce, line);
765                         
766                         recent = (RecentFile*)MEM_mallocN(sizeof(RecentFile),"RecentFile");
767                         BLI_addtail(&(G.recent_files), recent);
768                         recent->filename = (char*)MEM_mallocN(sizeof(char)*(strlen(line)+1), "name of file");
769                         recent->filename[0] = '\0';
770                         
771                         strcpy(recent->filename, line);
772                 }
773         }
774
775         if(G.sce[0] == 0)
776                 BLI_make_file_string("/", G.sce, BLI_gethome(), "untitled.blend");
777         
778         BLI_free_file_lines(lines);
779
780 #ifdef WIN32
781         /* Add the drive names to the listing */
782         {
783                 __int64 tmp;
784                 char folder[MAX_PATH];
785                 char tmps[4];
786                 int i;
787                         
788                 tmp= GetLogicalDrives();
789                 
790                 for (i=2; i < 26; i++) {
791                         if ((tmp>>i) & 1) {
792                                 tmps[0]='a'+i;
793                                 tmps[1]=':';
794                                 tmps[2]='\\';
795                                 tmps[3]=0;
796                                 
797                                 fsmenu_insert_entry(tmps, 0, 0);
798                         }
799                 }
800
801                 /* Adding Desktop and My Documents */
802                 fsmenu_append_separator();
803
804                 SHGetSpecialFolderPath(0, folder, CSIDL_PERSONAL, 0);
805                 fsmenu_insert_entry(folder, 0, 0);
806                 SHGetSpecialFolderPath(0, folder, CSIDL_DESKTOPDIRECTORY, 0);
807                 fsmenu_insert_entry(folder, 0, 0);
808
809                 fsmenu_append_separator();
810         }
811 #else
812         /* add home dir on linux systems */
813         fsmenu_insert_entry(BLI_gethome(), 0, 0);
814 #endif
815
816         BLI_make_file_string(G.sce, name, BLI_gethome(), ".Bfs");
817         lines= BLI_read_file_as_lines(name);
818
819         for (l= lines; l; l= l->next) {
820                 char *line= l->link;
821                         
822                 if (!BLI_streq(line, "")) {
823                         fsmenu_insert_entry(line, 0, 1);
824                 }
825         }
826
827         fsmenu_append_separator();
828         
829         /* add last saved file */
830         BLI_split_dirfile_basic(G.sce, name, filename); /* G.sce shouldn't be relative */
831         
832         fsmenu_insert_entry(name, 0, 0);
833         
834         BLI_free_file_lines(lines);
835 }
836
837 static void writeBlog(void)
838 {
839         struct RecentFile *recent, *next_recent;
840         char name[FILE_MAXDIR+FILE_MAXFILE];
841         FILE *fp;
842         int i;
843
844         BLI_make_file_string("/", name, BLI_gethome(), ".Blog");
845
846         recent = G.recent_files.first;
847         /* refresh .Blog of recent opened files, when current file was changed */
848         if(!(recent) || (strcmp(recent->filename, G.sce)!=0)) {
849                 fp= fopen(name, "w");
850                 if (fp) {
851                         /* add current file to the beginning of list */
852                         recent = (RecentFile*)MEM_mallocN(sizeof(RecentFile),"RecentFile");
853                         recent->filename = (char*)MEM_mallocN(sizeof(char)*(strlen(G.sce)+1), "name of file");
854                         recent->filename[0] = '\0';
855                         strcpy(recent->filename, G.sce);
856                         BLI_addhead(&(G.recent_files), recent);
857                         /* write current file to .Blog */
858                         fprintf(fp, "%s\n", recent->filename);
859                         recent = recent->next;
860                         i=1;
861                         /* write rest of recent opened files to .Blog */
862                         while((i<U.recent_files) && (recent)){
863                                 /* this prevents to have duplicities in list */
864                                 if (strcmp(recent->filename, G.sce)!=0) {
865                                         fprintf(fp, "%s\n", recent->filename);
866                                         recent = recent->next;
867                                 }
868                                 else {
869                                         next_recent = recent->next;
870                                         MEM_freeN(recent->filename);
871                                         BLI_freelinkN(&(G.recent_files), recent);
872                                         recent = next_recent;
873                                 }
874                                 i++;
875                         }
876                         fclose(fp);
877                 }
878         }
879 }
880
881 static void do_history(char *name)
882 {
883         char tempname1[FILE_MAXDIR+FILE_MAXFILE], tempname2[FILE_MAXDIR+FILE_MAXFILE];
884         int hisnr= U.versions;
885         
886         if(U.versions==0) return;
887         if(strlen(name)<2) return;
888                 
889         while(  hisnr > 1) {
890                 sprintf(tempname1, "%s%d", name, hisnr-1);
891                 sprintf(tempname2, "%s%d", name, hisnr);
892         
893                 if(BLI_rename(tempname1, tempname2))
894                         error("Unable to make version backup");
895                         
896                 hisnr--;
897         }
898                 
899         /* is needed when hisnr==1 */
900         sprintf(tempname1, "%s%d", name, hisnr);
901         
902         if(BLI_rename(name, tempname1))
903                 error("Unable to make version backup");
904 }
905
906 void BIF_write_file(char *target)
907 {
908         Library *li;
909         int writeflags, len;
910         char di[FILE_MAX];
911         char *err;
912         
913         len = strlen(target);
914         
915         if (len == 0) return;
916         if (len >= FILE_MAX) {
917                 error("Path too long, cannot save");
918                 return;
919         }
920  
921 #ifndef DISABLE_PYTHON
922         /* send the OnSave event */
923         if (G.f & G_DOSCRIPTLINKS) BPY_do_pyscript(&G.scene->id, SCRIPT_ONSAVE);
924 #endif
925
926         for (li= G.main->library.first; li; li= li->id.next) {
927                 if (li->parent==NULL && BLI_streq(li->name, target)) {
928                         error("Cannot overwrite used library");
929                         return;
930                 }
931         }
932         
933         if (!BLO_has_bfile_extension(target) && (len+6 < FILE_MAX)) {
934                 sprintf(di, "%s.blend", target);
935         } else {
936                 strcpy(di, target);
937         }
938
939         if (BLI_exists(di)) {
940                 if(!saveover(di))
941                         return; 
942         }
943         
944         if(G.obedit) {
945                 exit_editmode(0);       /* 0 = no free data */
946         }
947         if (G.fileflags & G_AUTOPACK) {
948                 packAll();
949         }
950         
951         waitcursor(1);  // exit_editmode sets cursor too
952
953         do_history(di);
954         
955         /* we use the UserDef to define compression flag */
956         writeflags= G.fileflags & ~G_FILE_COMPRESS;
957         if(U.flag & USER_FILECOMPRESS)
958                 writeflags |= G_FILE_COMPRESS;
959         
960         if (BLO_write_file(di, writeflags, &err)) {
961                 strcpy(G.sce, di);
962                 G.relbase_valid = 1;
963                 BLI_strncpy(G.main->name, di, FILE_MAX);        /* is guaranteed current file */
964
965                 mainwindow_set_filename_to_title(G.main->name);
966
967                 G.save_over = 1;
968
969                 writeBlog();
970         } else {
971                 error("failed to write blend file: %s", err);
972         }
973
974         waitcursor(0);
975 }
976
977 void BIF_write_homefile(void)
978 {
979         char *err, tstr[FILE_MAXDIR+FILE_MAXFILE];
980         int write_flags;
981         
982         BLI_make_file_string("/", tstr, BLI_gethome(), ".B.blend");
983                 
984         /*  force save as regular blend file */
985         write_flags = G.fileflags & ~(G_FILE_COMPRESS | G_FILE_LOCK | G_FILE_SIGN);
986         
987         if (!BLO_write_file(tstr, write_flags, &err)) {
988                 error("failed writing defaults: %s", err);
989         }
990 }
991
992 void BIF_write_autosave(void)
993 {
994         char *err, tstr[FILE_MAXDIR+FILE_MAXFILE];
995         int write_flags;
996         
997         get_autosave_location(tstr);
998
999                 /*  force save as regular blend file */
1000         write_flags = G.fileflags & ~(G_FILE_COMPRESS | G_FILE_LOCK | G_FILE_SIGN);
1001         if (!BLO_write_file(tstr, write_flags, &err)) {
1002                 fprintf(stderr, "failed to write autosave: %s\n", err); /* using error(...) is too annoying here */
1003         }
1004 }
1005
1006 /* remove temp files assosiated with this blend file when quitting, loading or saving in a new path */
1007 void BIF_clear_tempfiles( void )
1008 {
1009         /* TODO - remove exr files from the temp dir */
1010         
1011         if (!G.relbase_valid) { /* We could have pointcache saved in tyhe temp dir, if its there */
1012                 BKE_ptcache_remove();
1013         }
1014 }
1015
1016 /* if global undo; remove tempsave, otherwise rename */
1017 static void delete_autosave(void)
1018 {
1019         char tstr[FILE_MAXDIR+FILE_MAXFILE];
1020         
1021         get_autosave_location(tstr);
1022
1023         if (BLI_exists(tstr)) {
1024                 char str[FILE_MAXDIR+FILE_MAXFILE];
1025                 BLI_make_file_string("/", str, btempdir, "quit.blend");
1026
1027                 if(U.uiflag & USER_GLOBALUNDO) BLI_delete(tstr, 0, 0);
1028                 else BLI_rename(tstr, str);
1029         }
1030 }
1031
1032 /***/
1033
1034 static void initbuttons(void)
1035 {
1036         uiDefFont(UI_HELVB, 
1037                                 BMF_GetFont(BMF_kHelveticaBold14), 
1038                                 BMF_GetFont(BMF_kHelveticaBold12), 
1039                                 BMF_GetFont(BMF_kHelveticaBold10), 
1040                                 BMF_GetFont(BMF_kHelveticaBold8));
1041         uiDefFont(UI_HELV, 
1042                                 BMF_GetFont(BMF_kHelvetica12), 
1043                                 BMF_GetFont(BMF_kHelvetica12), 
1044                                 BMF_GetFont(BMF_kHelvetica10), 
1045                                 BMF_GetFont(BMF_kHelveticaBold8));
1046
1047         glClearColor(.7f, .7f, .6f, 0.0);
1048         
1049         G.font= BMF_GetFont(BMF_kHelvetica12);
1050         G.fonts= BMF_GetFont(BMF_kHelvetica10);
1051         G.fontss= BMF_GetFont(BMF_kHelveticaBold8);
1052
1053         clear_matcopybuf();
1054         
1055         glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
1056 }
1057
1058
1059 static void sound_init_listener(void)
1060 {
1061         G.listener = MEM_callocN(sizeof(bSoundListener), "soundlistener");
1062         G.listener->gain = 1.0;
1063         G.listener->dopplerfactor = 1.0;
1064         G.listener->dopplervelocity = 340.29f;
1065 }
1066
1067 void BIF_init(void)
1068 {
1069
1070         initscreen();   /* for (visuele) speed, this first, then setscreen */
1071         initbuttons();
1072         InitCursorData();
1073         sound_init_listener();
1074         init_node_butfuncs();
1075         
1076         BIF_preview_init_dbase();
1077         BIF_read_homefile(0);
1078
1079         BIF_resources_init();   /* after homefile, to dynamically load an icon file based on theme settings */
1080         
1081         BIF_filelist_init_icons();
1082
1083         GPU_state_init();
1084         GPU_extensions_init();
1085
1086         readBlog();
1087         BLI_strncpy(G.lib, G.sce, FILE_MAX);
1088 }
1089
1090 /***/
1091
1092 extern ListBase editNurb;
1093 extern ListBase editelems;
1094
1095 void exit_usiblender(void)
1096 {
1097         struct TmpFont *tf;
1098         int totblock;
1099         
1100         BIF_clear_tempfiles();
1101         
1102         tf= G.ttfdata.first;
1103         while(tf)
1104         {
1105                 freePackedFile(tf->pf);
1106                 tf->pf= NULL;
1107                 tf->vfont= NULL;
1108                 tf= tf->next;
1109         }
1110         BLI_freelistN(&G.ttfdata);
1111 #ifdef WITH_VERSE
1112         end_all_verse_sessions();
1113 #endif
1114         free_openrecent();
1115
1116         freeAllRad();
1117         BKE_freecubetable();
1118
1119         if (G.background == 0)
1120                 sound_end_all_sounds();
1121
1122         if(G.obedit) {
1123                 if(G.obedit->type==OB_FONT) {
1124                         free_editText();
1125                 }
1126                 else if(G.obedit->type==OB_MBALL) BLI_freelistN(&editelems);
1127                 free_editMesh(G.editMesh);
1128         }
1129
1130         free_editLatt();
1131         free_editArmature();
1132         free_posebuf();
1133
1134 #ifndef DISABLE_PYTHON
1135         /* before free_blender so py's gc happens while library still exists */
1136         /* needed at least for a rare sigsegv that can happen in pydrivers */
1137         BPY_end_python();
1138 #endif
1139
1140         fastshade_free_render();        /* shaded view */
1141         free_blender();                         /* blender.c, does entire library */
1142         free_matcopybuf();
1143         free_ipocopybuf();
1144         free_actcopybuf();
1145         free_gpcopybuf();
1146         free_vertexpaint();
1147         free_texttools();
1148         
1149         /* editnurb can remain to exist outside editmode */
1150         freeNurblist(&editNurb);
1151
1152         fsmenu_free();
1153
1154 #ifdef INTERNATIONAL
1155         free_languagemenu();
1156 #endif
1157         
1158         RE_FreeAllRender();
1159         
1160         free_txt_data();
1161
1162         sound_exit_audio();
1163         if(G.listener) MEM_freeN(G.listener);
1164
1165         GPU_extensions_exit();
1166
1167         libtiff_exit();
1168
1169 #ifdef WITH_QUICKTIME
1170         quicktime_exit();
1171 #endif
1172
1173         /* undo free stuff */
1174         undo_editmode_clear();
1175         undo_imagepaint_clear();
1176         
1177         BKE_undo_save_quit();   // saves quit.blend if global undo is on
1178         BKE_reset_undo(); 
1179         
1180         if (!G.background) {
1181                 BIF_resources_free();
1182                 
1183                 BIF_filelist_free_icons();
1184
1185                 BIF_free_render_spare();
1186                 BIF_close_render_display();
1187                 mainwindow_close();
1188         }
1189
1190 #ifdef INTERNATIONAL
1191         FTF_End();
1192 #endif
1193
1194         if (copybuf) MEM_freeN(copybuf);
1195         if (copybufinfo) MEM_freeN(copybufinfo);
1196
1197 //      
1198         BLI_freelistN(&U.themes);
1199         BIF_preview_free_dbase();
1200         
1201         totblock= MEM_get_memory_blocks_in_use();
1202         if(totblock!=0) {
1203                 printf("Error Totblock: %d\n",totblock);
1204                 MEM_printmemlist();
1205         }
1206         delete_autosave();
1207         
1208         printf("\nBlender quit\n");
1209
1210 #ifdef WIN32   
1211         /* ask user to press enter when in debug mode */
1212         if(G.f & G_DEBUG) {
1213                 printf("press enter key to exit...\n\n");
1214                 getchar();
1215         }
1216 #endif 
1217
1218
1219         SYS_DeleteSystem(SYS_GetSystem());
1220         
1221         exit(G.afbreek==1);
1222 }