/********************************************************

AVI player
Copyright 2000 Eugene Kuznetsov  (divx@euro.ru)

*********************************************************/

#include <version.h>

#include <signal.h>
#include <unistd.h>
#include <sys/time.h>
#include <fcntl.h>
#ifdef __FreeBSD__
#include <floatingpoint.h>
#endif
#ifdef __NetBSD__
#include <machine/ieeefp.h>
#endif

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

#include <qapp.h>
#include <qtimer.h>
#include "mywidget.h" // because of Qt

MyWidget *m;
extern void killer(int);

void Usage()
{
    cout <<
	"\nUsage: aviplay [options] [<avi-file>] [<subtitle-file>]\n"
	"\n  Options:\n"
	"    --rc          Enables remote control (stdin|stdout).\n"
	"    --size <x><y> Start playing movie at dimensions x * y.\n"
	"    --fullscreen  Start playing movie in fullscreen.\n"
	"    --maximize    Start playing movie in maximized mode.\n"
	"    --seek <sec>  Seek to sec position in the movie.\n"
	"    --quality [0,1,2,3,4,auto] Start playing movie at given quality.\n"
	"\n  Keys:\n"
	"    see About in GUI and man page for more details\n"
	"\n  More options are configurable inside the GUI\n"
        << endl;
}
int main(int argc, char **argv)
{
#if defined(__FreeBSD__) || defined(__NetBSD__)
    fpsetmask(fpgetmask() & ~(FP_X_DZ|FP_X_INV));
#endif
    double testfp = argc / 1.4;

    if (0 && (GetAvifileVersion() != AVIFILE_VERSION))
    {
	cout<<"This binary was compiled for Avifile ver. "
	    <<GetAvifileVersion()<<", but the library is ver. "
	    <<AVIFILE_VERSION<<". Aborting. "<< endl;
	return 0;
    }

    QApplication a(argc, argv);
    AviPlayerInitParams apip;

    int rc=0; // remote control support over stdin/stdout
    int picw=0, pich=0;

    //    QFont font=MyConfigDialog_impl::ReadFont();

    int use_quality = 1; // autotunning is 11 0 - 4 is quality
    bool run_fullscreen = false;

    int i = 0;
    while (++i < argc) {
	if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h"))
	{
	    Usage();
	    return 0;
	}
	else if (!strcmp(argv[i], "--size") || !strcmp(argv[i], "-size") || !strcmp(argv[i], "-s"))
	{
	    if (i + 2 >= argc)
	    {
		Usage();
		return -1;
	    }
	    apip.width = atoi(argv[++i]);
	    apip.height = atoi(argv[++i]);
	}
	else if (!strcmp(argv[i], "--rc") || !strcmp(argv[i], "-rc") || !strcmp(argv[i], "-r"))
	{
	    rc=1;
	    // non-buffered stdout
	    setvbuf (stdout, (char *)NULL, _IOLBF, 0);
	}
	else if (!strcmp(argv[i], "--fullscreen") || !strcmp(argv[i], "-f"))
	{
	    apip.fullscreen = true;
	}
	else if (!strcmp(argv[i], "--maximize") || !strcmp(argv[i], "-m"))
	{
	    apip.maximize = true;
	}
	else if (!strcmp(argv[i], "--seek") || !strcmp(argv[i], "-seek"))
	{
	    if (++i < argc)
	    {
		sscanf(argv[i], "%lf", &apip.seek);
                cout << "seeking to " << apip.seek << endl;
	    }
	}
	else if (!strcmp(argv[i], "--quality") || !strcmp(argv[i], "-q"))
	{
	    if (++i >= argc)
	    {
		Usage();
		return -1;
	    }
	    if (!strcmp(argv[i], "auto") || !strcmp(argv[i], "a"))
		apip.quality = 11;
	    else
	    {
		apip.quality = atoi(argv[i]);
		cout << "quality setting " << use_quality << " is not yet implemented..." << endl;
	    }
	}
	else
	    break;
    }

    //cout << "fullscreen " << run_fullscreen << endl;
    //cout << "quality " << use_quality << endl;

    char* fn;
    m = new MyWidget(384,30);
    try
    {
	if(i + 1 <= argc)
	{
	    apip.filename = argv[i++];
	    apip.subname = (i < argc) ? argv[i++] : NULL;
	    //m->setQuality(use_quality);
	    m->initPlayer(apip);
	}
	else m->open_func();
    }
    catch (FatalError& error)
    {
	if(argc >= 2)
	    error.Print();
    }

    // signal(SIGINT, killer);
    m->show();
    a.setMainWidget(m);
    QTimer t;
    if(rc)
    {
        QObject::connect(&t, SIGNAL(timeout()), m, SLOT(remote_command()));
        sleep (2);
	t.start(100);
    }
    a.exec();
    return 0;
}
/*
 vim: tabstop=8
 */
