#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <amiga/dev/grfioctl.h>

/*
  the above included grfioctl.h has been altered and is not yet 
  implemented in the main stream of NetBSD(-Amiga) kernel source.
  Although, where you found this source, this file is likely 
  been included, too. ill
*/

/*
   This is a very simple implementation for the new IOCTL i added
   into the kernel to toggle between Cirrus based boards display and
   native Amiga ECS display.

   Install in /usr/local/bin

   The boards - Picasso, Piccolo and Spectrum - all have a so-called
   pass-through register in their I/O-space to allow one-monitor solutions,
   using a relais to switch the source to the monitor.
   
   The kernel currently stores the active display in the variable 
   bah_toggle.

   The IOCTL is called 'GRFTOGGLE' and is only implemented on the above
   mentioned boards.
   
   A future Xcl X Server for Cirrus boards will probably use this IOCTL
   to toggle between the screens, until then you can execute this 
   programm from within your X environmnet, ie. your twm-menu. Add this
   line in the .twmrc:o
  
   "Toggle Display"    !"toggle &"

   somewhere inside a 'menu' enclosure.

   This thing is PD, Copyright (C) '94 by Markus 'ill' Illenseer,
   markus@techfak.uni-bielefeld.de

   Last change: 15/11/1994
*/

main(argc, argv)
 int argc;
 char *argv[];
{
 char *device,*def="/dev/grf3";
 int r;

 if(argc>=2) 
  {
   device=malloc(strlen(argv[1]));
   strcpy(device,argv[1]);
  }
 else
  {
   device=malloc(strlen(def));
   strcpy(device,def);
  }

 if(access (device, R_OK | W_OK) == -1)
  {
    printf("Cannot access device '%s'.\n",device);
    printf("Have you specified the correct graphics device?\n");
    return -1;
  }
  else
  { 
   if((r=open(device, O_RDWR, 0)) == -1)
    {
      printf("Cannot open device '%s'.\n",device);
      return -1;
    }
   else
    {
      if(ioctl(r,GRFTOGGLE,(unsigned short) 0) == -1)
       {
         printf("Cannot perform ioctl GRFTOGGLE on device '%s'.\n",device);
         (void) close(r);
         return -1;
       }
      (void) close(r);
    }
  }

 /* You miss free() ? This no Amiga-exec :-) */

}
