/* 
    glidetv - display picture on a 3dfx card 
              (3dfx card and glide library required)

    Copyright (C) 1997 Ralph Metzler (rjkm@thp.uni-koeln.de)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <fcntl.h>
#include <ioctls.h>
#include <glide.h>
#include "../driver/bttv.h"
#include "../driver/videodev.h"

struct video_window vwin;
struct video_buffer vbuf;

GrHwConfiguration hwconfig;

int fbttv;
char *bttvname="/dev/bttv";

/* set this to 640 x 480 for NTSC */

int width=768, height=576;

void fail(char *msg)
{
  printf(msg);
  grGlideShutdown();
  exit(1);
}

main( int argc, char **argv) {
  GrLfbInfo_t info;
  
  GrScreenResolution_t resolution = GR_RESOLUTION_800x600;
  float                scrWidth   = 800.0f;
  float                scrHeight  = 600.0f;
  int x,y;
  ulong dat=1;
  int one=1, zero=0;
  
  if ((fbttv=open(bttvname,O_RDWR)) < 0) {
    printf("Cannot open %s\n", bttvname);
    exit(1);
  }
  
  grGlideInit();
  assert( grSstQueryHardware( &hwconfig ) );
  grSstSelect( 0 );
  assert( grSstWinOpen( 0, resolution, GR_REFRESH_60Hz, GR_COLORFORMAT_ABGR,
		       GR_ORIGIN_UPPER_LEFT, 2, 1));
  grDitherMode( GR_DITHER_DISABLE );
  grClipWindow(0, 0, (FxU32) scrWidth, (FxU32) scrHeight);
  grBufferClear( 0x00, 0, 0 );
  info.size = sizeof( GrLfbInfo_t );
  if ( grLfbLock( GR_LFB_WRITE_ONLY, GR_BUFFER_FRONTBUFFER,
		 GR_LFBWRITEMODE_565, GR_ORIGIN_UPPER_LEFT, FXFALSE, &info )
      ==FXFALSE) {
    printf( "Error, failed to take write lock\n" );
    grGlideShutdown();
    exit(1);
  }
  printf("0x%08x \n",info.lfbPtr);
  printf("0x%08x \n",grTexMinAddress(GR_TMU0));
  printf("0x%08x \n",grTexMaxAddress(GR_TMU0));
  if(ioctl(fbttv, VIDIOCCAPTURE, &zero))
    fail("VIDIOCCAPTURE failed \n");

  if(ioctl(fbttv, VIDIOCGFBUF, &vbuf))
    fail("VIDIOCGFBUF failed \n");
  vbuf.base=(void *)(1|(unsigned long)info.lfbPtr);
  vbuf.depth=16;
  vbuf.bytesperline=info.strideInBytes;
  vbuf.width=800;
  vbuf.height=600;
  if(ioctl(fbttv, VIDIOCSFBUF, &vbuf))
    fail("VIDIOCSFBUF failed \n");

  vwin.clipcount=0;
  vwin.flags=0;
  vwin.y=0;
  vwin.x=0;
  vwin.width=width;
  vwin.height=height;
  if (ioctl(fbttv, VIDIOCSWIN, &vwin))
    fail("VIDIOCSWIN failed \n");

  if (ioctl(fbttv, VIDIOCSCHAN, &one))
    fail("VIDIOCSCHAN failed \n");
  
  if(ioctl(fbttv, VIDIOCCAPTURE,&one))
    fail("VIDIOCCAPTURE failed \n");
  
  while (!lin_getch());

  if(ioctl(fbttv, VIDIOCCAPTURE,&zero))
    fail("VIDIOCCAPTURE failed \n");
  grLfbUnlock( GR_LFB_WRITE_ONLY, GR_BUFFER_FRONTBUFFER );
  grGlideShutdown();
}

