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