#include <exec/types.h>
#include <exec/ports.h>
#include <exec/io.h>
#include <proto/exec.h>
#include <devices/keyboard.h>
#include <devices/inputevent.h>
#include <devices/input.h>

#include "ReadKeyboard.h"
#include "WBStartup+.h"

#include <proto/input.h>
#include <clib/input_protos.h>

struct Device *InputBase;
/*
BOOL IsQualifierDepressed(struct WBStartupPrefs *prefs)
{
  struct MsgPort *InputMP;
  struct IOStdReq *InputIO;
  UBYTE  matrix[13];
//  ULONG inputsigflag;
  BOOL success=FALSE;
  BOOL runprefs=FALSE;

  /////////////////////////
  // Set up input device //
  /////////////////////////
  if (InputMP=CreateMsgPort())
  {
    if (InputIO = CreateIORequest (InputMP, sizeof (struct IOStdReq)))
    {
      if ( !(OpenDevice("keyboard.device",0,(struct IORequest *) InputIO,0)) )
      {
        //inputsigflag = 1L << InputMP->mp_SigBit;

        InputIO->io_Length  = 13;
        InputIO->io_Command = KBD_READMATRIX;
        InputIO->io_Data = matrix;
        DoIO ((struct IORequest *) InputIO);

        if((matrix[96/8] & (1<<(96%8))))  // 96 (is the Raw Key number for Left Shift, p. 831 RKM
          success=TRUE;
        else if((matrix[99/8] & (1<<(99%8))))  // 99 is the Raw Key number for Ctrl
          runprefs=TRUE;
        else if((matrix[100/8] & (1<<(100%8)))) // 100 is the Raw Key number for Left Alt
          prefs->Interactive = TRUE;

        CloseDevice ((struct IORequest *) InputIO);
      }
      DeleteIORequest(InputIO);
    }
    DeleteMsgPort(InputMP);
  }

  if (runprefs)
    RunPrefs(prefs);
  return(success);
}
*/

//NEW

BOOL IsQualifierDepressed(struct WBStartupPrefs *prefs)
{
  struct IOStdReq *InputIO;
  struct MsgPort *InputMP;
  BOOL success=FALSE;
  BOOL runprefs=FALSE;

  /***********************/
  /* Set up input device */
  /***********************/

  if (InputMP=CreateMsgPort())
  {
    if (InputIO = CreateIORequest (InputMP, sizeof (struct IOStdReq)))
    {


  if (OpenDevice ("input.device", 0, (struct IORequest *) InputIO, 0))
    {
      UWORD data;

      InputBase = InputIO -> io_Device;

      data = PeekQualifier();

      if (data & (1 << IEQUALIFIER_MIDBUTTON))
        runprefs = TRUE;

        

      CloseDevice ((struct IORequest *) InputIO);
    }

      DeleteIORequest(InputIO);
    }
    DeleteMsgPort(InputMP);
  }

  if (runprefs)
    RunPrefs(prefs);
  return(success);
}
