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