Two fixes;
[blender.git] / source / blender / src / usiblender.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL/BL DUAL 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. 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
12  * about this.
13  *
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.
18  *
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.
22  *
23  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
24  * All rights reserved.
25  *
26  * The Original Code is: all of this file.
27  *
28  * Contributor(s): none yet.
29  *
30  * ***** END GPL/BL DUAL LICENSE BLOCK *****
31  */
32
33         /* placed up here because of crappy
34          * winsock stuff.
35          */
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <string.h>
39
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43
44 #ifdef WIN32
45 #include "BLI_winstuff.h"
46 #include <process.h> /* getpid */
47 #else
48 #include <unistd.h> /* getpid */
49 #endif
50 #include "MEM_guardedalloc.h"
51
52 #include "BMF_Api.h"
53 #ifdef INTERNATIONAL
54 #include "BIF_language.h"
55 #include "FTF_Api.h"
56 #endif
57
58 #include "BLI_blenlib.h"
59 #include "BLI_arithb.h"
60 #include "BLI_linklist.h"
61
62 #include "IMB_imbuf_types.h"
63 #include "IMB_imbuf.h"
64
65 #include "DNA_object_types.h"
66 #include "DNA_space_types.h"
67 #include "DNA_userdef_types.h"
68 #include "DNA_sound_types.h"
69
70 #include "BKE_blender.h"
71 #include "BKE_curve.h"
72 #include "BKE_displist.h"
73 #include "BKE_exotic.h"
74 #include "BKE_font.h"
75 #include "BKE_global.h"
76 #include "BKE_main.h"
77 #include "BKE_mball.h"
78 #include "BKE_packedFile.h"
79 #include "BKE_utildefines.h"
80
81 #include "BIF_fsmenu.h"
82 #include "BIF_gl.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_editmode_undo.h"
91 #include "BIF_editsound.h"
92 #include "BIF_renderwin.h"
93 #include "BIF_resources.h"
94 #include "BIF_screen.h"
95 #include "BIF_space.h"
96 #include "BIF_toolbox.h"
97 #include "BIF_cursors.h"
98
99 #include "BSE_drawview.h"
100 #include "BSE_headerbuttons.h"
101 #include "BSE_editipo.h"
102 #include "BSE_editaction.h"
103 #include "BSE_filesel.h"
104 #include "BSE_edit.h"
105
106 #include "BLO_readfile.h"
107 #include "BLO_writefile.h"
108
109 #include "BDR_drawobject.h"
110 #include "BDR_editobject.h"
111 #include "BDR_editcurve.h"
112 #include "BDR_vpaint.h"
113
114 #include "BPY_extern.h"
115
116 #include "blendef.h"
117
118 #include "radio.h"
119 #include "render.h"
120 #include "datatoc.h"
121
122 #include "SYS_System.h"
123
124 #include "PIL_time.h"
125
126 /***/
127
128 void BIF_read_file(char *name)
129 {
130         extern short winqueue_break; /* editscreen.c */
131
132         //here?
133         //sound_end_all_sounds();
134
135         // first try to read exotic file formats...
136         if (BKE_read_exotic(name) == 0) { /* throws first error box */
137                 /* we didn't succeed, now try to read Blender file
138                    calls readfile, calls toolbox, throws one more,
139                    on failure calls the stream, and that is stubbed.... */
140                 BKE_read_file(name, NULL);
141         }
142
143         mainwindow_set_filename_to_title(G.main->name);
144         countall();
145         sound_initialize_sounds();
146
147         winqueue_break= 1;      /* leave queues everywhere */
148
149         undo_editmode_clear();
150         BKE_reset_undo();
151         BKE_write_undo("original");     /* save current state */
152 }
153
154 int BIF_read_homefile(void)
155 {
156         char tstr[FILE_MAXDIR+FILE_MAXFILE], scestr[FILE_MAXDIR];
157         char *home= BLI_gethome();
158         int success;
159 #ifdef _WIN32   // FULLSCREEN
160         static int screenmode = -1;
161
162         screenmode = U.uiflag & USER_FLIPFULLSCREEN;
163 #endif
164
165         BLI_make_file_string(G.sce, tstr, home, ".B.blend");
166         strcpy(scestr, G.sce);  /* temporal store */
167
168         /* only here free userdef themes... */
169         BLI_freelistN(&U.themes);
170
171         if (BLI_exists(tstr)) {
172                 success = BKE_read_file(tstr, NULL);
173         } else {
174                 success = BKE_read_file_from_memory(datatoc_B_blend, datatoc_B_blend_size, NULL);
175         }
176         strcpy(G.sce, scestr);
177
178         BIF_InitTheme();        // sets default again
179
180         if (success) {
181                 mainwindow_set_filename_to_title(tstr);
182                 countall();
183                 G.save_over = 0;
184
185                 /*  disable autoplay in .B.blend... */
186                 G.fileflags &= ~G_FILE_AUTOPLAY;
187
188 #ifdef _WIN32   // FULLSCREEN
189                 /* choose window startmode */
190                 switch (G.windowstate){
191                         case G_WINDOWSTATE_USERDEF: /* use the usersetting */
192                                 break;
193                         case G_WINDOWSTATE_FULLSCREEN: /* force fullscreen */
194                                 U.uiflag |= USER_FLIPFULLSCREEN;
195                                 break;
196                         case G_WINDOWSTATE_BORDER: /* force with borders */
197                                 U.uiflag &= ~USER_FLIPFULLSCREEN;
198                 }
199
200                 if(screenmode != (U.uiflag & USER_FLIPFULLSCREEN)) {
201                         mainwindow_toggle_fullscreen ((U.uiflag & USER_FLIPFULLSCREEN));
202                         screenmode = (U.uiflag & USER_FLIPFULLSCREEN);
203                 }
204 #endif
205
206                 if (BLI_streq(U.tempdir, "/")) {
207                         char *tmp= getenv("TEMP");
208
209                         strcpy(U.tempdir, tmp?tmp:"/tmp/");
210                 }
211                 if (U.savetime <= 0) {
212                         U.savetime = 1;
213                         error("%s is buggy, please consider removing it.\n",
214                                 tstr);
215                 }
216                 if (G.main->versionfile <= 191) {
217                         strcpy(U.plugtexdir, U.textudir);
218                         strcpy(U.sounddir, "/");
219                 }
220
221                         /* patch to set Dupli Armature */
222                 if (G.main->versionfile < 220) {
223                         U.dupflag |= USER_DUP_ARM;
224                 }
225
226                         /* userdef new option */
227                 if (G.main->versionfile <= 222) {
228                         U.vrmlflag= USER_VRML_LAYERS;
229                 }
230
231                         /* added seam, normal color */
232                 if (G.main->versionfile <= 234) {
233                         bTheme *btheme;
234                         
235                         for(btheme= U.themes.first; btheme; btheme= btheme->next) {
236                                 /* check for alpha==0 is safe, then color was never set */
237                                 if(btheme->tv3d.edge_seam[3]==0) {
238                                         btheme->tv3d.edge_seam[0]= 230;
239                                         btheme->tv3d.edge_seam[1]= 150;
240                                         btheme->tv3d.edge_seam[2]= 50;
241                                         btheme->tv3d.edge_seam[3]= 255;
242                                 }
243                                 if(btheme->tv3d.normal[3]==0) {
244                                         btheme->tv3d.normal[0]= 0x22;
245                                         btheme->tv3d.normal[1]= 0xDD;
246                                         btheme->tv3d.normal[2]= 0xDD;
247                                         btheme->tv3d.normal[3]= 255;
248                                 }
249                                 if(btheme->tv3d.face_dot[3]==0) {
250                                         btheme->tv3d.face_dot[0]= 255;
251                                         btheme->tv3d.face_dot[1]= 138;
252                                         btheme->tv3d.face_dot[2]= 48;
253                                         btheme->tv3d.face_dot[3]= 255;
254                                         btheme->tv3d.facedot_size= 4;
255                                 }
256                         }
257                 }
258                 
259                 space_set_commmandline_options();
260
261                 if (U.undosteps==0) U.undosteps=32;
262                 undo_editmode_clear();
263                 BKE_reset_undo();
264                 BKE_write_undo("original");     /* save current state */
265
266                 reset_autosave();
267
268 #ifdef INTERNATIONAL
269                 read_languagefile();
270         
271                 if(U.transopts & USER_DOTRANSLATE)
272                         start_interface_font();
273                 else
274                         G.ui_international = FALSE;
275 #endif // INTERNATIONAL
276
277         }
278
279         return success;
280 }
281
282 static void get_autosave_location(char buf[FILE_MAXDIR+FILE_MAXFILE])
283 {
284         char pidstr[32];
285
286         sprintf(pidstr, "%d.blend", abs(getpid()));
287         BLI_make_file_string("/", buf, U.tempdir, pidstr);
288 }
289
290 void BIF_read_autosavefile(void)
291 {
292         char tstr[FILE_MAXDIR+FILE_MAXFILE], scestr[FILE_MAXDIR];
293         int save_over;
294
295         strcpy(scestr, G.sce);  /* temporal store */
296         
297         get_autosave_location(tstr);
298
299         save_over = G.save_over;
300         BKE_read_file(tstr, NULL);
301         G.save_over = save_over;
302         strcpy(G.sce, scestr);
303 }
304
305 /***/
306
307 static void readBlog(void)
308 {
309         char name[FILE_MAXDIR+FILE_MAXFILE];
310         LinkNode *l, *lines;
311
312         BLI_make_file_string("/", name, BLI_gethome(), ".Blog");
313         lines= BLI_read_file_as_lines(name);
314
315         if (lines && !BLI_streq(lines->link, "")) {
316                 strcpy(G.sce, lines->link);
317         } else {
318                 BLI_make_file_string("/", G.sce, BLI_gethome(), "untitled.blend");
319         }
320
321         BLI_free_file_lines(lines);
322
323 #ifdef WIN32
324         /* Add the drive names to the listing */
325         {
326                 __int64 tmp;
327                 char tmps[4];
328                 int i;
329                         
330                 tmp= GetLogicalDrives();
331                 
332                 for (i=2; i < 26; i++) {
333                         if ((tmp>>i) & 1) {
334                                 tmps[0]='a'+i;
335                                 tmps[1]=':';
336                                 tmps[2]='\\';
337                                 tmps[3]=0;
338                                 
339                                 fsmenu_insert_entry(tmps, 0);
340                         }
341                 }
342                 
343                 fsmenu_append_seperator();
344         }
345 #endif
346
347         BLI_make_file_string(G.sce, name, BLI_gethome(), ".Bfs");
348         lines= BLI_read_file_as_lines(name);
349
350         for (l= lines; l; l= l->next) {
351                 char *line= l->link;
352                         
353                 if (!BLI_streq(line, "")) {
354                         fsmenu_insert_entry(line, 0);
355                 }
356         }
357
358         fsmenu_append_seperator();
359         BLI_free_file_lines(lines);
360 }
361
362
363 static void writeBlog(void)
364 {
365         char name[FILE_MAXDIR+FILE_MAXFILE];
366         FILE *fp;
367
368         BLI_make_file_string("/", name, BLI_gethome(), ".Blog");
369
370         fp= fopen(name, "w");
371         if (fp) {
372                 fprintf(fp, G.sce);
373                 fclose(fp);
374         }
375 }
376
377 static void do_history(char *name)
378 {
379         char tempname1[FILE_MAXDIR+FILE_MAXFILE], tempname2[FILE_MAXDIR+FILE_MAXFILE];
380         int hisnr= U.versions;
381         
382         if(U.versions==0) return;
383         if(strlen(name)<2) return;
384                 
385         while(  hisnr > 1) {
386                 sprintf(tempname1, "%s%d", name, hisnr-1);
387                 sprintf(tempname2, "%s%d", name, hisnr);
388         
389                 if(BLI_rename(tempname1, tempname2))
390                         error("Unable to make version backup");
391                         
392                 hisnr--;
393         }
394                 
395         /* is needed when hisnr==1 */
396         sprintf(tempname1, "%s%d", name, hisnr);
397         
398         if(BLI_rename(name, tempname1))
399                 error("Unable to make version backup");
400 }
401
402 void BIF_write_file(char *target)
403 {
404         Library *li;
405         char di[FILE_MAXDIR];
406         char *err;
407         
408         if (BLI_streq(target, "")) return;
409  
410         /*Send the OnSave event*/
411         if (G.f & G_SCENESCRIPT) {
412                 BPY_do_pyscript(&G.scene->id, SCRIPT_ONSAVE);
413         }
414
415         for (li= G.main->library.first; li; li= li->id.next) {
416                 if (BLI_streq(li->name, target)) {
417                         error("Cannot overwrite used library");
418                         return;
419                 }
420         }
421         
422         if (!BLO_has_bfile_extension(target)) {
423                 sprintf(di, "%s.blend", target);
424         } else {
425                 strcpy(di, target);
426         }
427
428         if (BLI_exists(di)) {
429                 if(!saveover(di))
430                         return; 
431         }
432         
433         waitcursor(1);
434         
435         if(G.obedit) {
436                 exit_editmode(0);       /* 0 = no free data */
437         }
438         if (G.fileflags & G_AUTOPACK) {
439                 packAll();
440         }
441
442         do_history(di);
443                 
444         if (BLO_write_file(di, G.fileflags, &err)) {
445                 strcpy(G.sce, di);
446                 strcpy(G.main->name, di);       /* is guaranteed current file */
447
448                 mainwindow_set_filename_to_title(G.main->name);
449
450                 G.save_over = 1;
451
452                 writeBlog();
453         } else {
454                 error("%s", err);
455         }
456
457         waitcursor(0);
458 }
459
460 void BIF_write_homefile(void)
461 {
462         char *err, tstr[FILE_MAXDIR+FILE_MAXFILE];
463         int write_flags;
464         
465         BLI_make_file_string("/", tstr, BLI_gethome(), ".B.blend");
466                 
467         /*  force save as regular blend file */
468         write_flags = G.fileflags & ~(G_FILE_COMPRESS | G_FILE_LOCK | G_FILE_SIGN);
469         BLO_write_file(tstr, write_flags, &err);
470 }
471
472 void BIF_write_autosave(void)
473 {
474         char *err, tstr[FILE_MAXDIR+FILE_MAXFILE];
475         int write_flags;
476         
477         get_autosave_location(tstr);
478
479                 /*  force save as regular blend file */
480         write_flags = G.fileflags & ~(G_FILE_COMPRESS | G_FILE_LOCK | G_FILE_SIGN);
481         BLO_write_file(tstr, write_flags, &err);
482 }
483
484 /* if global undo; remove tempsave, otherwise rename */
485 static void delete_autosave(void)
486 {
487         char tstr[FILE_MAXDIR+FILE_MAXFILE];
488         
489         get_autosave_location(tstr);
490
491         if (BLI_exists(tstr)) {
492                 char str[FILE_MAXDIR+FILE_MAXFILE];
493                 BLI_make_file_string("/", str, U.tempdir, "quit.blend");
494
495                 if(U.uiflag & USER_GLOBALUNDO) BLI_delete(tstr, 0, 0);
496                 else BLI_rename(tstr, str);
497         }
498 }
499
500 /***/
501
502 static void initbuttons(void)
503 {
504         uiDefFont(UI_HELVB, 
505                                 BMF_GetFont(BMF_kHelveticaBold14), 
506                                 BMF_GetFont(BMF_kHelveticaBold12), 
507                                 BMF_GetFont(BMF_kHelveticaBold10), 
508                                 BMF_GetFont(BMF_kHelveticaBold8));
509         uiDefFont(UI_HELV, 
510                                 BMF_GetFont(BMF_kHelvetica12), 
511                                 BMF_GetFont(BMF_kHelvetica12), 
512                                 BMF_GetFont(BMF_kHelvetica10), 
513                                 BMF_GetFont(BMF_kHelveticaBold8));
514         
515         BIF_resources_init();
516
517         glClearColor(.7, .7, .6, 0.0);
518         
519         G.font= BMF_GetFont(BMF_kHelvetica12);
520         G.fonts= BMF_GetFont(BMF_kHelvetica10);
521         G.fontss= BMF_GetFont(BMF_kHelveticaBold8);
522
523         clear_matcopybuf();
524 }
525
526
527 static void sound_init_listener(void)
528 {
529         G.listener = MEM_callocN(sizeof(bSoundListener), "soundlistener");
530         G.listener->gain = 1.0;
531         G.listener->dopplerfactor = 1.0;
532         G.listener->dopplervelocity = 1.0;
533 }
534
535 void BIF_init(void)
536 {
537
538         initscreen();   /* for (visuele) speed, this first, then setscreen */
539         initbuttons();
540         InitCursorData();
541         sound_init_listener();
542         
543         init_draw_rects();      /* drawobject.c */
544         BIF_read_homefile();
545         init_gl_stuff();        /* drawview.c, after homefile */
546         readBlog();
547         strcpy(G.lib, G.sce);
548
549 }
550
551 /***/
552
553 extern ListBase editNurb;
554 extern ListBase editelems;
555
556 void exit_usiblender(void)
557 {
558         freeAllRad();
559         BKE_freecubetable();
560
561         if (G.background == 0)
562                 sound_end_all_sounds();
563
564         if(G.obedit) {
565                 if(G.obedit->type==OB_FONT) {
566                         free_editText();
567                 }
568                 else if(G.obedit->type==OB_MBALL) BLI_freelistN(&editelems);
569                 free_editMesh(G.editMesh);
570         }
571
572         free_editLatt();
573         free_editArmature();
574         free_posebuf();
575
576         free_blender(); /* blender.c, does entire library */
577         free_matcopybuf();
578         free_ipocopybuf();
579         freefastshade();
580         free_vertexpaint();
581         
582         /* editnurb can remain to exist outside editmode */
583         freeNurblist(&editNurb);
584
585         fsmenu_free();
586 #ifdef INTERNATIONAL
587         free_languagemenu();
588 #endif  
589         
590         RE_free_render_data();
591         RE_free_filt_mask();
592         
593         free_txt_data();
594
595         sound_exit_audio();
596         if(G.listener) MEM_freeN(G.listener);
597
598 #ifdef WITH_QUICKTIME
599         quicktime_exit();
600 #endif
601                 
602         BPY_end_python();
603
604         if (!G.background) {
605                 BIF_resources_free();
606         
607                 BIF_close_render_display();
608                 mainwindow_close();
609         }
610
611 #ifdef INTERNATIONAL
612         FTF_End();
613 #endif
614
615         /* undo free stuff */
616         undo_editmode_clear();
617         
618         BKE_undo_save_quit();   // saves quit.blend if global undo is on
619         BKE_reset_undo(); 
620         
621         BLI_freelistN(&U.themes);
622         
623         if(totblock!=0) {
624                 printf("Error Totblock: %d\n",totblock);
625                 MEM_printmemlist();
626         }
627         delete_autosave();
628         
629         printf("\nBlender quit\n");
630
631 #ifdef WIN32   
632         /* ask user to press enter when in debug mode */
633         if(G.f & G_DEBUG) {
634                 printf("press enter key to exit...\n\n");
635                 getchar();
636         }
637 #endif 
638
639
640         SYS_DeleteSystem(SYS_GetSystem());
641
642         exit(G.afbreek==1);
643 }