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