2.5 audio cleanup:
[blender.git] / source / creator / creator.c
index dffa755b2648bf5e4e78cc4862b272c8b948f6db..8896673b6112767095724ec0894ef3a7baa201e3 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 
+
+/* for setuid / getuid */
+#ifdef __sgi
+#include <sys/types.h>
+#include <unistd.h>
+#endif
+
 /* This little block needed for linking to Blender... */
 
 #include "MEM_guardedalloc.h"
 #include "DNA_scene_types.h"
 
 #include "BLI_blenlib.h"
-#include "blendef.h" /* for MAXFRAME */
-
 
 #include "BKE_utildefines.h"
 #include "BKE_blender.h"
+#include "BKE_context.h"
 #include "BKE_font.h"
 #include "BKE_global.h"
 #include "BKE_main.h"
 #include "BKE_packedFile.h"
 #include "BKE_scene.h"
 #include "BKE_node.h"
-
-#include "BIF_gl.h"
-#include "BIF_graphics.h"
-#include "BIF_mainqueue.h"
-#include "BIF_graphics.h"
-#include "BIF_editsound.h"
-#include "BIF_usiblender.h"
-#include "BIF_drawscene.h"      /* set_scene() */
-#include "BIF_screen.h"         /* waitcursor and more */
-#include "BIF_usiblender.h"
-#include "BIF_toolbox.h"
-
-#include "BLO_writefile.h"
-#include "BLO_readfile.h"
+#include "BKE_report.h"
 
 #include "IMB_imbuf.h" // for quicktime_init
 
+#ifndef DISABLE_PYTHON
 #include "BPY_extern.h"
+#endif
 
 #include "RE_pipeline.h"
 
-#include "GPU_draw.h"
+//XXX #include "playanim_ext.h"
+#include "ED_datafiles.h"
+#include "UI_interface.h"
+
+#include "WM_api.h"
 
-#include "playanim_ext.h"
-#include "mydevice.h"
-#include "nla.h"
-#include "datatoc.h"
+#include "RNA_define.h"
+
+#include "GPU_draw.h"
+#include "GPU_extensions.h"
 
 /* for passing information between creator and gameengine */
 #include "SYS_System.h"
@@ -107,18 +106,8 @@ extern char * build_type;
 #endif
 
 /*     Local Function prototypes */
-static void print_help();
-static void print_version();
-
-
-/* defined in ghostwinlay and winlay, we can't include carbon here, conflict with DNA */
-#ifdef __APPLE__
-extern int checkAppleVideoCard(void);
-extern void getMacAvailableBounds(short *top, short *left, short *bottom, short *right);
-extern void    winlay_get_screensize(int *width_r, int *height_r);
-extern void    winlay_process_events(int wait_for_event);
-#endif
-
+static void print_help(void);
+static void print_version(void);
 
 /* for the callbacks: */
 
@@ -128,7 +117,7 @@ char bprogname[FILE_MAXDIR+FILE_MAXFILE]; /* from blenpluginapi:pluginapi.c */
 char btempdir[FILE_MAXDIR+FILE_MAXFILE];
 
 /* Initialise callbacks for the modules that need them */
-void setCallbacks(void); 
+static void setCallbacks(void); 
 
 #if defined(__sgi) || defined(__alpha__)
 static void fpe_handler(int sig)
@@ -157,20 +146,20 @@ static void blender_esc(int sig)
 static void print_version(void)
 {
 #ifdef BUILD_DATE
-       printf ("Blender %d.%02d (sub %d) Build\n", G.version/100, G.version%100, BLENDER_SUBVERSION);
+       printf ("Blender %d.%02d (sub %d) Build\n", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
        printf ("\tbuild date: %s\n", build_date);
        printf ("\tbuild time: %s\n", build_time);
        printf ("\tbuild revision: %s\n", build_rev);
        printf ("\tbuild platform: %s\n", build_platform);
        printf ("\tbuild type: %s\n", build_type);
 #else
-       printf ("Blender %d.%02d (sub %d) Build\n", G.version/100, G.version%100, BLENDER_SUBVERSION);
+       printf ("Blender %d.%02d (sub %d) Build\n", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
 #endif
 }
 
 static void print_help(void)
 {
-       printf ("Blender %d.%02d (sub %d) Build\n", G.version/100, G.version%100, BLENDER_SUBVERSION);
+       printf ("Blender %d.%02d (sub %d) Build\n", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
        printf ("Usage: blender [args ...] [file] [args ...]\n");
        printf ("\nRender options:\n");
        printf ("  -b <file>\tRender <file> in background (doesn't load the user defaults .B.blend file)\n");
@@ -198,7 +187,7 @@ static void print_help(void)
        printf ("    -t <threads>\tUse amount of <threads> for rendering (background mode only).\n");
        printf ("      [1-8], 0 for systems processor count.\n");
        printf ("\nAnimation playback options:\n");
-       printf ("  -a <file(s)>\tPlayback <file(s)>, only operates this way when -b is not used.\n");
+       printf ("  -a <options> <file(s)>\tPlayback <file(s)>, only operates this way when -b is not used.\n");
        printf ("    -p <sx> <sy>\tOpen with lower left corner at <sx>, <sy>\n");
        printf ("    -m\t\tRead from disk (Don't buffer)\n");
        printf ("    -f <fps> <fps-base>\t\tSpecify FPS to start with\n");
@@ -212,15 +201,15 @@ static void print_help(void)
        printf ("\nGame Engine specific options:\n");
        printf ("  -g fixedtime\t\tRun on 50 hertz without dropping frames\n");
        printf ("  -g vertexarrays\tUse Vertex Arrays for rendering (usually faster)\n");
-       printf ("  -g noaudio\t\tNo audio in Game Engine\n");
        printf ("  -g nomipmap\t\tNo Texture Mipmapping\n");
        printf ("  -g linearmipmap\tLinear Texture Mipmapping instead of Nearest (default)\n");
 
        printf ("\nMisc options:\n");
        printf ("  -d\t\tTurn debugging on\n");
-       printf ("  -noaudio\tDisable audio on systems that support audio\n");
+       printf ("  -nojoystick\tDisable joystick support\n");
+       printf ("  -noglsl\tDisable GLSL shading\n");
        printf ("  -h\t\tPrint this help text\n");
-       printf ("  -y\t\tDisable automatic python script execution (scriptlinks, pydrivers, pyconstraints, pynodes)\n");
+       printf ("  -y\t\tDisable automatic python script execution (pydrivers, pyconstraints, pynodes)\n");
        printf ("  -P <filename>\tRun the given Python script (filename or Blender Text)\n");
 #ifdef WIN32
        printf ("  -R\t\tRegister .blend extension\n");
@@ -234,8 +223,10 @@ static void print_help(void)
        printf ("  $TEMP\t\tStore temporary files here.\n");
 #else
        printf ("  $TMP or $TMPDIR\tStore temporary files here.\n");
-       printf ("  $SDL_AUDIODRIVER\tLibSDL audio driver - alsa, esd, alsa, dma.\n");
        printf ("  $BF_TIFF_LIB\t\tUse an alternative libtiff.so for loading tiff image files.\n");
+#endif
+#ifndef DISABLE_SDL
+       printf ("  $SDL_AUDIODRIVER\tLibSDL audio driver - alsa, esd, alsa, dma.\n");
 #endif
        printf ("  $IMAGEEDITOR\t\tImage editor executable, launch with the IKey from the file selector.\n");
        printf ("  $WINEDITOR\t\tText editor executable, launch with the EKey from the file selector.\n");
@@ -255,29 +246,25 @@ static void print_help(void)
 
 
 double PIL_check_seconds_timer(void);
-extern void winlay_get_screensize(int *width_r, int *height_r);
 
-static void main_init_screen( void )
+/* XXX This was here to fix a crash when running python scripts
+ * with -P that used the screen.
+ *
+ * static void main_init_screen( void )
 {
        setscreen(G.curscreen);
        
        if(G.main->scene.first==0) {
                set_scene( add_scene("1") );
        }
-}
+}*/
 
 int main(int argc, char **argv)
 {
-       int a, i, stax, stay, sizx, sizy, scr_init = 0;
        SYS_SystemHandle syshandle;
+       bContext *C= CTX_create();
+       int a, i, stax, stay, sizx, sizy /*XXX, scr_init = 0*/;
 
-#if defined(WIN32) || defined (__linux__)
-       int audio = 1;
-#else
-       int audio = 0;
-#endif
-
-       
 #ifdef WITH_BINRELOC
        br_init( NULL );
 #endif
@@ -288,27 +275,9 @@ int main(int argc, char **argv)
        if (argc==2 && strncmp(argv[1], "-psn_", 5)==0) {
                extern int GHOST_HACK_getFirstFile(char buf[]);
                static char firstfilebuf[512];
-               int scr_x,scr_y;
 
                argc= 1;
 
-        /* first let us check if we are hardware accelerated and with VRAM > 16 Mo */
-        
-        if (checkAppleVideoCard()) {
-                       short top, left, bottom, right;
-                       
-                       winlay_get_screensize(&scr_x, &scr_y); 
-                       getMacAvailableBounds(&top, &left, &bottom, &right);
-                       setprefsize(left +10,scr_y - bottom +10,right-left -20,bottom - 64, 0);
-
-        } else {
-                       winlay_get_screensize(&scr_x, &scr_y);
-
-                       /* 40 + 684 + (headers) 22 + 22 = 768, the powerbook screen height */
-                       setprefsize(120, 40, 850, 684, 0);
-        }
-    
-               winlay_process_events(0);
                if (GHOST_HACK_getFirstFile(firstfilebuf)) {
                        argc= 2;
                        argv[1]= firstfilebuf;
@@ -323,10 +292,6 @@ int main(int argc, char **argv)
 #ifdef __linux__
     #ifdef __alpha__
        signal (SIGFPE, fpe_handler);
-    #else
-       if ( getenv("SDL_AUDIODRIVER") == NULL) {
-               setenv("SDL_AUDIODRIVER", "alsa", 1);
-       }
     #endif
 #endif
 #if defined(__sgi)
@@ -338,11 +303,14 @@ int main(int argc, char **argv)
 
        BLI_where_am_i(bprogname, argv[0]);
        
+       RNA_init();
+       RE_engines_init();
+
                /* Hack - force inclusion of the plugin api functions,
                 * see blenpluginapi:pluginapi.c
                 */
        pluginapi_force_ref();
-       
+
        init_nodesystem();
        
        initglobals();  /* blender.c */
@@ -378,7 +346,11 @@ int main(int argc, char **argv)
                else if(argv[a][0] == '-') {
                        switch(argv[a][1]) {
                        case 'a': /* -b was not given, play an animation */
-                               playanim(argc-1, argv+1);
+                               
+                               /* exception here, see below, it probably needs happens after qt init? */
+                               libtiff_init();
+
+// XXX                         playanim(argc-1, argv+1);
                                exit(0);
                                break;
                        case 'b':
@@ -425,17 +397,13 @@ int main(int argc, char **argv)
 
        /* for all platforms, even windos has it! */
        if(G.background) signal(SIGINT, blender_esc);   /* ctrl c out bg render */
-
+       
        /* background render uses this font too */
        BKE_font_register_builtin(datatoc_Bfont, datatoc_Bfont_size);
        
        init_def_material();
 
        if(G.background==0) {
-               winlay_get_screensize(&sizx, &sizy);
-               stax=0;
-               stay=0;
-
                for(a=1; a<argc; a++) {
                        if(argv[a][0] == '-') {
                                switch(argv[a][1]) {
@@ -452,12 +420,12 @@ int main(int argc, char **argv)
                                        sizx= atoi(argv[a]);
                                        a++;
                                        sizy= atoi(argv[a]);
-                                       G.windowstate = G_WINDOWSTATE_BORDER;
 
+                                       WM_setprefsize(stax, stay, sizx, sizy);
                                        break;
                                case 'd':
                                        G.f |= G_DEBUG;         /* std output printf's */ 
-                                       printf ("Blender %d.%02d (sub %d) Build\n", G.version/100, G.version%100, BLENDER_SUBVERSION);
+                                       printf ("Blender %d.%02d (sub %d) Build\n", BLENDER_VERSION/100, BLENDER_VERSION%100, BLENDER_SUBVERSION);
                                        MEM_set_memory_debug();
 #ifdef NAN_BUILDINFO
                                        printf("Build: %s %s %s %s\n", build_date, build_time, build_platform, build_type);
@@ -467,13 +435,15 @@ int main(int argc, char **argv)
                                                printf("argv[%d] = %s\n", i, argv[i]);
                                        }
                                        break;
-            
+                               case 'E':
+                                       G.f |= G_SWAP_EXCHANGE;
+                                       break;
                                case 'w':
-                                       G.windowstate = G_WINDOWSTATE_BORDER;
+                                       /* with borders XXX OLD CRUFT!*/
                                        break;
                                case 'W':
+                                       /* borderless, win + linux XXX OLD CRUFT */
                                        /* XXX, fixme mein, borderless on OSX */
-                                       G.windowstate = G_WINDOWSTATE_FULLSCREEN;
                                        break;
                                case 'R':
                                        /* Registering filetypes only makes sense on windows...      */
@@ -483,59 +453,62 @@ int main(int argc, char **argv)
                                        break;
                                case 'n':
                                case 'N':
-                                       if (BLI_strcasecmp(argv[a], "-noaudio") == 0|| BLI_strcasecmp(argv[a], "-nosound") == 0) {
+                                       if (BLI_strcasecmp(argv[a], "-nojoystick") == 0) {
                                                /**
-                                                       notify the gameengine that no audio is wanted, even if the user didn't give
-                                                       the flag -g noaudio.
+                                                       don't initialize joysticks if user doesn't want to use joysticks
+                                                       failed joystick initialization delays over 5 seconds, before game engine start
                                                */
-
-                                               SYS_WriteCommandLineInt(syshandle,"noaudio",1);
-                                               audio = 0;
-                                               if (G.f & G_DEBUG) printf("setting audio to: %d\n", audio);
+                                               SYS_WriteCommandLineInt(syshandle,"nojoystick",1);
+                                               if (G.f & G_DEBUG) printf("disabling nojoystick\n");
                                        }
+                                       if (BLI_strcasecmp(argv[a], "-noglsl") == 0)
+                                               GPU_extensions_disable();
                                        break;
                                }
                        }
                }
 
-               if ( (G.windowstate == G_WINDOWSTATE_BORDER) || (G.windowstate == G_WINDOWSTATE_FULLSCREEN)) 
-                       setprefsize(stax, stay, sizx, sizy, 0);
-               
+#ifndef DISABLE_PYTHON         
                BPY_start_python(argc, argv);
-               
-               /**
-                * NOTE: sound_init_audio() *must be* after start_python,
-                * at least on FreeBSD.
-                * added note (ton): i removed it altogether
-                */
+#endif         
 
-               BIF_init(); /* loads .B.blend */
+               WM_init(C);
                
+               // XXX BRECHT SOLVE
                BLI_where_is_temp( btempdir, 1 ); /* call after loading the .B.blend so we can read U.tempdir */
 
+#ifndef DISABLE_SDL
+       BLI_setenv("SDL_VIDEODRIVER", "dummy");
+#ifdef __linux__
+       /* On linux the default SDL driver dma often would not play
+        * use alsa if none is set */
+       setenv("SDL_AUDIODRIVER", "alsa", 0);
+#endif
+#endif
        }
        else {
+#ifndef DISABLE_PYTHON
                BPY_start_python(argc, argv);
-               
+#endif         
                BLI_where_is_temp( btempdir, 0 ); /* call after loading the .B.blend so we can read U.tempdir */
-               
-               // (ton) Commented out. I have no idea whats thisfor... will mail around!
-               // SYS_WriteCommandLineInt(syshandle,"noaudio",1);
-        // audio = 0;
-        // sound_init_audio();
-        // if (G.f & G_DEBUG) printf("setting audio to: %d\n", audio);
        }
-
+#ifndef DISABLE_PYTHON
        /**
-        * NOTE: the U.pythondir string is NULL until BIF_init() is executed,
+        * NOTE: the U.pythondir string is NULL until WM_init() is executed,
         * so we provide the BPY_ function below to append the user defined
         * pythondir to Python's sys.path at this point.  Simply putting
-        * BIF_init() before BPY_start_python() crashes Blender at startup.
+        * WM_init() before BPY_start_python() crashes Blender at startup.
         * Update: now this function also inits the bpymenus, which also depend
         * on U.pythondir.
         */
        BPY_post_start_python();
 
+       BPY_run_ui_scripts(C, 0); /* dont need to reload the first time */
+#endif
+       
+       CTX_py_init_set(C, 1);
+       WM_keymap_init(C); /* after BPY_run_ui_scripts() */
+
 #ifdef WITH_QUICKTIME
 
        quicktime_init();
@@ -616,33 +589,26 @@ int main(int argc, char **argv)
                                }
                        case 'f':
                                a++;
-                               if (G.scene) {
+                               if (CTX_data_scene(C)) {
+                                       Scene *scene= CTX_data_scene(C);
+                                       
                                        if (a < argc) {
-                                               int frame= MIN2(MAXFRAME, MAX2(1, atoi(argv[a])));
-                                               Render *re= RE_NewRender(G.scene->id.name);
-
-                                               if (G.f & G_DOSCRIPTLINKS)
-                                                       BPY_do_all_scripts(SCRIPT_RENDER, 0);
-
-                                               RE_BlenderAnim(re, G.scene, frame, frame, G.scene->frame_step);
+                                               int frame = atoi(argv[a]);
+                                               Render *re = RE_NewRender(scene->id.name);
 
-                                               BPY_do_all_scripts(SCRIPT_POSTRENDER, 0);
+                                               frame = MIN2(MAXFRAME, MAX2(1, frame));
+                                               
+                                               RE_BlenderAnim(re, scene, frame, frame, scene->frame_step);
                                        }
                                } else {
                                        printf("\nError: no blend loaded. cannot use '-f'.\n");
                                }
                                break;
                        case 'a':
-                               if (G.scene) {
-                                       Render *re= RE_NewRender(G.scene->id.name);
-
-                                       if (G.f & G_DOSCRIPTLINKS)
-                                               BPY_do_all_scripts(SCRIPT_RENDER, 1);
-
-                                       RE_BlenderAnim(re, G.scene, G.scene->r.sfra, G.scene->r.efra, G.scene->frame_step);
-
-                                       if (G.f & G_DOSCRIPTLINKS)
-                                               BPY_do_all_scripts(SCRIPT_POSTRENDER, 1);
+                               if (CTX_data_scene(C)) {
+                                       Scene *scene= CTX_data_scene(C);
+                                       Render *re= RE_NewRender(scene->id.name);
+                                       RE_BlenderAnim(re, scene, scene->r.sfra, scene->r.efra, scene->frame_step);
                                } else {
                                        printf("\nError: no blend loaded. cannot use '-a'.\n");
                                }
@@ -654,32 +620,48 @@ int main(int argc, char **argv)
                                break;
                        case 's':
                                a++;
-                               if(G.scene) {
-                                       int frame= MIN2(MAXFRAME, MAX2(1, atoi(argv[a])));
-                                       if (a < argc) (G.scene->r.sfra) = frame;
+                               if (CTX_data_scene(C)) {
+                                       Scene *scene= CTX_data_scene(C);
+                                       if (a < argc) {
+                                               int frame = atoi(argv[a]);
+                                               (scene->r.sfra) = MIN2(MAXFRAME, MAX2(1, frame));
+                                       }
                                } else {
                                        printf("\nError: no blend loaded. cannot use '-s'.\n");
                                }
                                break;
                        case 'e':
                                a++;
-                               if(G.scene) {
-                                       int frame= MIN2(MAXFRAME, MAX2(1, atoi(argv[a])));
-                                       if (a < argc) (G.scene->r.efra) = frame;
+                               if (CTX_data_scene(C)) {
+                                       Scene *scene= CTX_data_scene(C);
+                                       if (a < argc) {
+                                               int frame = atoi(argv[a]);
+                                               (scene->r.efra) = MIN2(MAXFRAME, MAX2(1, frame));
+                                       }
                                } else {
                                        printf("\nError: no blend loaded. cannot use '-e'.\n");
                                }
                                break;
                        case 'j':
                                a++;
-                               if(G.scene) {
-                                       int fstep= MIN2(MAXFRAME, MAX2(1, atoi(argv[a])));
-                                       if (a < argc) (G.scene->frame_step) = fstep;
+                               if (CTX_data_scene(C)) {
+                                       Scene *scene= CTX_data_scene(C);
+                                       if (a < argc) {
+                                               int frame = atoi(argv[a]);
+                                               (scene->frame_step) = MIN2(MAXFRAME, MAX2(1, frame));
+                                       }
                                } else {
                                        printf("\nError: no blend loaded. cannot use '-j'.\n");
                                }
                                break;
                        case 'P':
+
+#ifndef DISABLE_PYTHON
+                               //XXX 
+                               // FOR TESTING ONLY
+                               a++;
+                               BPY_run_python_script(C, argv[a], NULL, NULL); // use reports?
+#if 0
                                a++;
                                if (a < argc) {
                                        /* If we're not running in background mode, then give python a valid screen */
@@ -687,15 +669,20 @@ int main(int argc, char **argv)
                                                main_init_screen();
                                                scr_init = 1;
                                        }
-                                       BPY_run_python_script (argv[a]);
+                                       BPY_run_python_script(C, argv[a], NULL, NULL); // use reports?
                                }
                                else printf("\nError: you must specify a Python script after '-P '.\n");
+#endif
+#else
+                               printf("This blender was built without python support\n");
+#endif /* DISABLE_PYTHON */
                                break;
                        case 'o':
                                a++;
                                if (a < argc){
-                                       if(G.scene) {
-                                               BLI_strncpy(G.scene->r.pic, argv[a], FILE_MAXDIR);
+                                       if (CTX_data_scene(C)) {
+                                               Scene *scene= CTX_data_scene(C);
+                                               BLI_strncpy(scene->r.pic, argv[a], FILE_MAXDIR);
                                        } else {
                                                printf("\nError: no blend loaded. cannot use '-o'.\n");
                                        }
@@ -706,35 +693,39 @@ int main(int argc, char **argv)
                        case 'F':
                                a++;
                                if (a < argc){
-                                       if(!G.scene) {
+                                       if (CTX_data_scene(C)==NULL) {
                                                printf("\nError: no blend loaded. order the arguments so '-F ' is after the blend is loaded.\n");
                                        } else {
-                                               if      (!strcmp(argv[a],"TGA")) G.scene->r.imtype = R_TARGA;
-                                               else if (!strcmp(argv[a],"IRIS")) G.scene->r.imtype = R_IRIS;
-                                               else if (!strcmp(argv[a],"HAMX")) G.scene->r.imtype = R_HAMX;
+                                               Scene *scene= CTX_data_scene(C);
+                                               if      (!strcmp(argv[a],"TGA")) scene->r.imtype = R_TARGA;
+                                               else if (!strcmp(argv[a],"IRIS")) scene->r.imtype = R_IRIS;
+                                               else if (!strcmp(argv[a],"HAMX")) scene->r.imtype = R_HAMX;
 #ifdef WITH_DDS
-                                               else if (!strcmp(argv[a],"DDS")) G.scene->r.imtype = R_DDS;
+                                               else if (!strcmp(argv[a],"DDS")) scene->r.imtype = R_DDS;
 #endif
-                                               else if (!strcmp(argv[a],"JPEG")) G.scene->r.imtype = R_JPEG90;
-                                               else if (!strcmp(argv[a],"MOVIE")) G.scene->r.imtype = R_MOVIE;
-                                               else if (!strcmp(argv[a],"IRIZ")) G.scene->r.imtype = R_IRIZ;
-                                               else if (!strcmp(argv[a],"RAWTGA")) G.scene->r.imtype = R_RAWTGA;
-                                               else if (!strcmp(argv[a],"AVIRAW")) G.scene->r.imtype = R_AVIRAW;
-                                               else if (!strcmp(argv[a],"AVIJPEG")) G.scene->r.imtype = R_AVIJPEG;
-                                               else if (!strcmp(argv[a],"PNG")) G.scene->r.imtype = R_PNG;
-                                               else if (!strcmp(argv[a],"AVICODEC")) G.scene->r.imtype = R_AVICODEC;
-                                               else if (!strcmp(argv[a],"QUICKTIME")) G.scene->r.imtype = R_QUICKTIME;
-                                               else if (!strcmp(argv[a],"BMP")) G.scene->r.imtype = R_BMP;
-                                               else if (!strcmp(argv[a],"HDR")) G.scene->r.imtype = R_RADHDR;
-                                               else if (!strcmp(argv[a],"TIFF")) G.scene->r.imtype = R_TIFF;
+                                               else if (!strcmp(argv[a],"JPEG")) scene->r.imtype = R_JPEG90;
+                                               else if (!strcmp(argv[a],"MOVIE")) scene->r.imtype = R_MOVIE;
+                                               else if (!strcmp(argv[a],"IRIZ")) scene->r.imtype = R_IRIZ;
+                                               else if (!strcmp(argv[a],"RAWTGA")) scene->r.imtype = R_RAWTGA;
+                                               else if (!strcmp(argv[a],"AVIRAW")) scene->r.imtype = R_AVIRAW;
+                                               else if (!strcmp(argv[a],"AVIJPEG")) scene->r.imtype = R_AVIJPEG;
+                                               else if (!strcmp(argv[a],"PNG")) scene->r.imtype = R_PNG;
+                                               else if (!strcmp(argv[a],"AVICODEC")) scene->r.imtype = R_AVICODEC;
+                                               else if (!strcmp(argv[a],"QUICKTIME")) scene->r.imtype = R_QUICKTIME;
+                                               else if (!strcmp(argv[a],"BMP")) scene->r.imtype = R_BMP;
+                                               else if (!strcmp(argv[a],"HDR")) scene->r.imtype = R_RADHDR;
+                                               else if (!strcmp(argv[a],"TIFF")) scene->r.imtype = R_TIFF;
 #ifdef WITH_OPENEXR
-                                               else if (!strcmp(argv[a],"EXR")) G.scene->r.imtype = R_OPENEXR;
-                                               else if (!strcmp(argv[a],"MULTILAYER")) G.scene->r.imtype = R_MULTILAYER;
+                                               else if (!strcmp(argv[a],"EXR")) scene->r.imtype = R_OPENEXR;
+                                               else if (!strcmp(argv[a],"MULTILAYER")) scene->r.imtype = R_MULTILAYER;
+#endif
+                                               else if (!strcmp(argv[a],"MPEG")) scene->r.imtype = R_FFMPEG;
+                                               else if (!strcmp(argv[a],"FRAMESERVER")) scene->r.imtype = R_FRAMESERVER;
+                                               else if (!strcmp(argv[a],"CINEON")) scene->r.imtype = R_CINEON;
+                                               else if (!strcmp(argv[a],"DPX")) scene->r.imtype = R_DPX;
+#if WITH_OPENJPEG
+                                               else if (!strcmp(argv[a],"JP2")) scene->r.imtype = R_JP2;
 #endif
-                                               else if (!strcmp(argv[a],"MPEG")) G.scene->r.imtype = R_FFMPEG;
-                                               else if (!strcmp(argv[a],"FRAMESERVER")) G.scene->r.imtype = R_FRAMESERVER;
-                                               else if (!strcmp(argv[a],"CINEON")) G.scene->r.imtype = R_CINEON;
-                                               else if (!strcmp(argv[a],"DPX")) G.scene->r.imtype = R_DPX;
                                                else printf("\nError: Format from '-F' not known or not compiled in this release.\n");
                                        }
                                } else {
@@ -744,20 +735,25 @@ int main(int argc, char **argv)
                                
                        case 't':
                                a++;
-                               if(G.background) {
-                                       RE_set_max_threads(atoi(argv[a]));
+                               if (a < argc) {
+                                       if(G.background) {
+                                               RE_set_max_threads(atoi(argv[a]));
+                                       } else {
+                                               printf("Warning: threads can only be set in background mode\n");
+                                       }
                                } else {
-                                       printf("Warning: threads can only be set in background mode\n");
+                                       printf("\nError: you must specify a number of threads between 0 and 8 '-t '.\n");
                                }
                                break;
                        case 'x': /* extension */
                                a++;
                                if (a < argc) {
-                                       if(G.scene) {
+                                       if (CTX_data_scene(C)) {
+                                               Scene *scene= CTX_data_scene(C);
                                                if (argv[a][0] == '0') {
-                                                       G.scene->r.scemode &= ~R_EXTENSION;
+                                                       scene->r.scemode &= ~R_EXTENSION;
                                                } else if (argv[a][0] == '1') {
-                                                       G.scene->r.scemode |= R_EXTENSION;
+                                                       scene->r.scemode |= R_EXTENSION;
                                                } else {
                                                        printf("\nError: Use '-x 1' or '-x 0' To set the extension option.\n");
                                                }
@@ -779,50 +775,54 @@ int main(int argc, char **argv)
                        BLI_convertstringcwd(filename);
                        
                        if (G.background) {
-                               int retval = BKE_read_file(filename, NULL);
-                               sound_initialize_sounds();
+                               int retval = BKE_read_file(C, argv[a], NULL, NULL);
+// XXX                 sound_initialize_sounds();
                                
                                /*we successfully loaded a blend file, get sure that
                                pointcache works */
                                if (retval!=0) G.relbase_valid = 1;
 
-                               /* happens for the UI on file reading too */
-                               BKE_reset_undo();
-                               BKE_write_undo("original");     /* save current state */
+                               /* happens for the UI on file reading too (huh? (ton))*/
+// XXX                 BKE_reset_undo();
+//                             BKE_write_undo("original");     /* save current state */
                        } else {
                                /* we are not running in background mode here, but start blender in UI mode with 
                                   a file - this should do everything a 'load file' does */
-                               BIF_read_file(filename);
+                               WM_read_file(C, filename, NULL);
                        }
                }
        }
-
+       
        if(G.background) {
                /* actually incorrect, but works for now (ton) */
-               exit_usiblender();
+               WM_exit(C);
        }
+
+
+       WM_main(C);
        
-       if (scr_init==0) {
+       /*XXX if (scr_init==0) {
                main_init_screen();
        }
        
-       screenmain(); /* main display loop */
+       screenmain();*/ /* main display loop */
 
        return 0;
 } /* end of int main(argc,argv)        */
 
 static void error_cb(char *err)
 {
-       error("%s", err);
+       
+       printf("%s\n", err);    /* XXX do this in WM too */
 }
 
 static void mem_error_cb(char *errorStr)
 {
-       fprintf(stderr, errorStr);
+       fputs(errorStr, stderr);
        fflush(stderr);
 }
 
-void setCallbacks(void)
+static void setCallbacks(void)
 {
        /* Error output from the alloc routines: */
        MEM_set_error_callback(mem_error_cb);
@@ -831,6 +831,6 @@ void setCallbacks(void)
        /* BLI_blenlib: */
 
        BLI_setErrorCallBack(error_cb); /* */
-       BLI_setInterruptCallBack(blender_test_break);
+// XXX BLI_setInterruptCallBack(blender_test_break);
 
 }