4 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
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. The Blender
10 * Foundation also sells licenses for use in proprietary software under
11 * the Blender License. See http://www.blender.org/BL/ for information
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24 * All rights reserved.
26 * The Original Code is: all of this file.
28 * Contributor(s): none yet.
30 * ***** END GPL/BL DUAL LICENSE BLOCK *****
33 /* placed up here because of crappy
45 #include "BLI_winstuff.h"
46 #include <process.h> /* getpid */
48 #include <unistd.h> /* getpid */
50 #include "MEM_guardedalloc.h"
54 #include "BIF_language.h"
58 #include "BLI_blenlib.h"
59 #include "BLI_arithb.h"
60 #include "BLI_linklist.h"
62 #include "IMB_imbuf_types.h"
63 #include "IMB_imbuf.h"
65 #include "DNA_object_types.h"
66 #include "DNA_space_types.h"
67 #include "DNA_userdef_types.h"
68 #include "DNA_sound_types.h"
70 #include "BKE_blender.h"
71 #include "BKE_curve.h"
72 #include "BKE_displist.h"
73 #include "BKE_exotic.h"
75 #include "BKE_global.h"
77 #include "BKE_mball.h"
78 #include "BKE_packedFile.h"
79 #include "BKE_utildefines.h"
81 #include "BIF_fsmenu.h"
83 #include "BIF_interface.h"
84 #include "BIF_usiblender.h"
85 #include "BIF_drawtext.h"
86 #include "BIF_editarmature.h"
87 #include "BIF_editlattice.h"
88 #include "BIF_editfont.h"
89 #include "BIF_editmesh.h"
90 #include "BIF_editsound.h"
91 #include "BIF_renderwin.h"
92 #include "BIF_resources.h"
93 #include "BIF_screen.h"
94 #include "BIF_space.h"
95 #include "BIF_toolbox.h"
96 #include "BIF_cursors.h"
98 #include "BSE_drawview.h"
99 #include "BSE_headerbuttons.h"
100 #include "BSE_editipo.h"
101 #include "BSE_editaction.h"
102 #include "BSE_filesel.h"
103 #include "BSE_edit.h"
105 #include "BLO_readfile.h"
106 #include "BLO_writefile.h"
108 #include "BDR_drawobject.h"
109 #include "BDR_editobject.h"
110 #include "BDR_editcurve.h"
111 #include "BDR_vpaint.h"
113 #include "BPY_extern.h"
121 #include "SYS_System.h"
123 #include "PIL_time.h"
125 // temporal, will go to include file
126 void BIF_reset_undo(void);
127 void BIF_write_undo(char *);
132 void BIF_read_file(char *name)
134 extern short winqueue_break; /* editscreen.c */
137 //sound_end_all_sounds();
139 // first try to read exotic file formats...
140 if (BKE_read_exotic(name) == 0) { /* throws first error box */
141 /* we didn't succeed, now try to read Blender file
142 calls readfile, calls toolbox, throws one more,
143 on failure calls the stream, and that is stubbed.... */
144 BKE_read_file(name, NULL);
147 mainwindow_set_filename_to_title(G.main->name);
149 sound_initialize_sounds();
151 winqueue_break= 1; /* leave queues everywhere */
154 BIF_write_undo("original"); /* save current state */
157 int BIF_read_homefile(void)
159 char tstr[FILE_MAXDIR+FILE_MAXFILE], scestr[FILE_MAXDIR];
160 char *home= BLI_gethome();
162 #ifdef _WIN32 // FULLSCREEN
163 static int screenmode = -1;
165 screenmode = U.uiflag & USER_FLIPFULLSCREEN;
168 BLI_make_file_string(G.sce, tstr, home, ".B.blend");
169 strcpy(scestr, G.sce); /* temporal store */
171 /* only here free userdef themes... */
172 BLI_freelistN(&U.themes);
174 if (BLI_exists(tstr)) {
175 success = BKE_read_file(tstr, NULL);
177 success = BKE_read_file_from_memory(datatoc_B_blend, datatoc_B_blend_size, NULL);
179 strcpy(G.sce, scestr);
181 BIF_InitTheme(); // sets default again
184 mainwindow_set_filename_to_title(tstr);
188 /* disable autoplay in .B.blend... */
189 G.fileflags &= ~G_FILE_AUTOPLAY;
191 #ifdef _WIN32 // FULLSCREEN
192 /* choose window startmode */
193 switch (G.windowstate){
194 case G_WINDOWSTATE_USERDEF: /* use the usersetting */
196 case G_WINDOWSTATE_FULLSCREEN: /* force fullscreen */
197 U.uiflag |= USER_FLIPFULLSCREEN;
199 case G_WINDOWSTATE_BORDER: /* force with borders */
200 U.uiflag &= ~USER_FLIPFULLSCREEN;
203 if(screenmode != (U.uiflag & USER_FLIPFULLSCREEN)) {
204 mainwindow_toggle_fullscreen ((U.uiflag & USER_FLIPFULLSCREEN));
205 screenmode = (U.uiflag & USER_FLIPFULLSCREEN);
209 if (BLI_streq(U.tempdir, "/")) {
210 char *tmp= getenv("TEMP");
212 strcpy(U.tempdir, tmp?tmp:"/tmp/");
214 if (U.savetime <= 0) {
216 error("%s is buggy, please cosider removing it.\n",
219 if (G.main->versionfile <= 191) {
220 strcpy(U.plugtexdir, U.textudir);
221 strcpy(U.sounddir, "/");
224 /* patch to set Dupli Armature */
225 if (G.main->versionfile < 220) {
226 U.dupflag |= USER_DUP_ARM;
229 /* userdef new option */
230 if (G.main->versionfile <= 222) {
231 U.vrmlflag= USER_VRML_LAYERS;
234 space_set_commmandline_options();
236 if (U.undosteps==0) U.undosteps=32;
238 BIF_write_undo("original"); /* save current state */
245 if(U.transopts & USER_DOTRANSLATE)
246 start_interface_font();
248 G.ui_international = FALSE;
249 #endif // INTERNATIONAL
256 static void get_autosave_location(char buf[FILE_MAXDIR+FILE_MAXFILE])
260 sprintf(pidstr, "%d.blend", abs(getpid()));
261 BLI_make_file_string("/", buf, U.tempdir, pidstr);
264 void BIF_read_autosavefile(void)
266 char tstr[FILE_MAXDIR+FILE_MAXFILE], scestr[FILE_MAXDIR];
269 strcpy(scestr, G.sce); /* temporal store */
271 get_autosave_location(tstr);
273 save_over = G.save_over;
274 BKE_read_file(tstr, NULL);
275 G.save_over = save_over;
276 strcpy(G.sce, scestr);
281 static void readBlog(void)
283 char name[FILE_MAXDIR+FILE_MAXFILE];
286 BLI_make_file_string("/", name, BLI_gethome(), ".Blog");
287 lines= BLI_read_file_as_lines(name);
289 if (lines && !BLI_streq(lines->link, "")) {
290 strcpy(G.sce, lines->link);
292 BLI_make_file_string("/", G.sce, BLI_gethome(), "untitled.blend");
295 BLI_free_file_lines(lines);
298 /* Add the drive names to the listing */
304 tmp= GetLogicalDrives();
306 for (i=2; i < 26; i++) {
313 fsmenu_insert_entry(tmps, 0);
317 fsmenu_append_seperator();
321 BLI_make_file_string(G.sce, name, BLI_gethome(), ".Bfs");
322 lines= BLI_read_file_as_lines(name);
324 for (l= lines; l; l= l->next) {
327 if (!BLI_streq(line, "")) {
328 fsmenu_insert_entry(line, 0);
332 fsmenu_append_seperator();
333 BLI_free_file_lines(lines);
337 static void writeBlog(void)
339 char name[FILE_MAXDIR+FILE_MAXFILE];
342 BLI_make_file_string("/", name, BLI_gethome(), ".Blog");
344 fp= fopen(name, "w");
351 static void do_history(char *name)
353 char tempname1[FILE_MAXDIR+FILE_MAXFILE], tempname2[FILE_MAXDIR+FILE_MAXFILE];
354 int hisnr= U.versions;
356 if(U.versions==0) return;
357 if(strlen(name)<2) return;
360 sprintf(tempname1, "%s%d", name, hisnr-1);
361 sprintf(tempname2, "%s%d", name, hisnr);
363 if(BLI_rename(tempname1, tempname2))
364 error("Unable to make version backup");
369 /* is needed when hisnr==1 */
370 sprintf(tempname1, "%s%d", name, hisnr);
372 if(BLI_rename(name, tempname1))
373 error("Unable to make version backup");
376 void BIF_write_file(char *target)
379 char di[FILE_MAXDIR];
382 if (BLI_streq(target, "")) return;
384 /*Send the OnSave event*/
385 if (G.f & G_SCENESCRIPT) {
386 BPY_do_pyscript(&G.scene->id, SCRIPT_ONSAVE);
389 for (li= G.main->library.first; li; li= li->id.next) {
390 if (BLI_streq(li->name, target)) {
391 error("Cannot overwrite used library");
396 if (!BLO_has_bfile_extension(target)) {
397 sprintf(di, "%s.blend", target);
402 if (BLI_exists(di)) {
410 exit_editmode(0); /* 0 = no free data */
412 if (G.fileflags & G_AUTOPACK) {
418 if (BLO_write_file(di, G.fileflags, &err)) {
420 strcpy(G.main->name, di); /* is guaranteed current file */
422 mainwindow_set_filename_to_title(G.main->name);
434 void BIF_write_homefile(void)
436 char *err, tstr[FILE_MAXDIR+FILE_MAXFILE];
439 BLI_make_file_string("/", tstr, BLI_gethome(), ".B.blend");
441 /* force save as regular blend file */
442 write_flags = G.fileflags & ~(G_FILE_COMPRESS | G_FILE_LOCK | G_FILE_SIGN);
443 BLO_write_file(tstr, write_flags, &err);
446 void BIF_write_autosave(void)
448 char *err, tstr[FILE_MAXDIR+FILE_MAXFILE];
451 get_autosave_location(tstr);
453 /* force save as regular blend file */
454 write_flags = G.fileflags & ~(G_FILE_COMPRESS | G_FILE_LOCK | G_FILE_SIGN);
455 BLO_write_file(tstr, write_flags, &err);
458 static void delete_autosave(void)
460 char tstr[FILE_MAXDIR+FILE_MAXFILE];
462 get_autosave_location(tstr);
464 if (BLI_exists(tstr)) {
465 char str[FILE_MAXDIR+FILE_MAXFILE];
466 BLI_make_file_string("/", str, U.tempdir, "quit.blend");
467 BLI_rename(tstr, str);
473 static void initbuttons(void)
476 BMF_GetFont(BMF_kHelveticaBold14),
477 BMF_GetFont(BMF_kHelveticaBold12),
478 BMF_GetFont(BMF_kHelveticaBold10),
479 BMF_GetFont(BMF_kHelveticaBold8));
481 BMF_GetFont(BMF_kHelvetica12),
482 BMF_GetFont(BMF_kHelvetica12),
483 BMF_GetFont(BMF_kHelvetica10),
484 BMF_GetFont(BMF_kHelveticaBold8));
486 BIF_resources_init();
488 glClearColor(.7, .7, .6, 0.0);
490 G.font= BMF_GetFont(BMF_kHelvetica12);
491 G.fonts= BMF_GetFont(BMF_kHelvetica10);
492 G.fontss= BMF_GetFont(BMF_kHelveticaBold8);
498 static void sound_init_listener(void)
500 G.listener = MEM_callocN(sizeof(bSoundListener), "soundlistener");
501 G.listener->gain = 1.0;
502 G.listener->dopplerfactor = 1.0;
503 G.listener->dopplervelocity = 1.0;
509 initscreen(); /* for (visuele) speed, this first, then setscreen */
512 sound_init_listener();
514 init_draw_rects(); /* drawobject.c */
516 init_gl_stuff(); /* drawview.c, after homefile */
518 strcpy(G.lib, G.sce);
524 extern ListBase editNurb;
525 extern ListBase editelems;
527 void exit_usiblender(void)
532 if (G.background == 0)
533 sound_end_all_sounds();
536 if(G.obedit->type==OB_FONT) {
539 else if(G.obedit->type==OB_MBALL) BLI_freelistN(&editelems);
547 free_blender(); /* blender.c, does entire library */
554 /* editnurb can remain to exist outside editmode */
555 freeNurblist(&editNurb);
562 RE_free_render_data();
568 if(G.listener) MEM_freeN(G.listener);
570 #ifdef WITH_QUICKTIME
577 BIF_resources_free();
579 BIF_close_render_display();
587 if (G.undo_clear) G.undo_clear();
591 BLI_freelistN(&U.themes);
594 printf("Error Totblock: %d\n",totblock);
599 printf("\nBlender quit\n");
602 /* ask user to press enter when in debug mode */
604 printf("press enter key to exit...\n\n");
610 SYS_DeleteSystem(SYS_GetSystem());