#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>

#include <clib/alib_protos.h>
#include <stdio.h>
#include <string.h>

#include <clib/utility_protos.h>
#include <proto/utility.h>

extern struct Device *InputBase;
extern struct Library *UtilityBase;

// to evaluate the user-requested hotkeys
struct MyHotkey
{
  char *name;
  UWORD value;
};


/*
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 Screen *scr_p)
{
  struct IORequest *InputIO;
  struct MsgPort *InputMP;
  BOOL success = FALSE;
  BOOL runprefs = FALSE;

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

  if (InputMP = CreatePort (NULL, 0))
    {
      if (InputIO = CreateExtIO (InputMP, sizeof (struct IOStdReq)))
        {
          if (!OpenDevice ("input.device", 0, (struct IORequest *) InputIO, 0))
           {
              UWORD data;
              //char buffer [6];

              InputBase = (struct Device *) InputIO -> io_Device;

              data = PeekQualifier();

              /*
              sprintf (buffer, "%x", data);
              ShowRequester (buffer);

              // check user input

              ShowRequester (prefs -> RunPrefsQual);
              ShowRequester (prefs -> DisableQual);
              ShowRequester (prefs -> InteractiveQual);
              */

              if (data & (GetQualName (prefs -> RunPrefsQual)))
                {
                  runprefs = TRUE;
                }
              else if (data & (GetQualName (prefs -> DisableQual)))
                {
                  success = TRUE;
                }
              else if (data & (GetQualName (prefs -> InteractiveQual)))
                {
                  prefs -> Interactive = TRUE;
                }

              CloseDevice ((struct IORequest *) InputIO);
            }
          else
            ShowRequester ("failed to open input device");

          DeleteIORequest (InputIO);
        }
      else
        ShowRequester ("Failed to create IORequest");

      DeleteMsgPort (InputMP);
    }
  else
    ShowRequester ("failed to create msg port");

  if (runprefs)
    RunPrefs (prefs, scr_p);

  return(success);
}


WORD GetQualName (const char *name)
{
  struct MyHotkey hotkeys [10] =
  {
    {"LSHIFT",      0x0001},
    {"RSHIFT",      0x0002},
    {"CONTROL",     0x0008},
    {"LALT",        0x0010},
    {"RALT",        0x0020},
    {"LCOMMAND",    0x0040},
    {"RCOMMAND",    0x0080},
    {"MIDBUTTON",   0x1000},
    {"RBUTTON",     0x2000},
    {"LEFTBUTTON",  0x4000}
  };
  UBYTE success = FALSE;

  // find user prefs
  WORD i = 0;
  while (i < 10)
    {
      if (! (Stricmp (hotkeys [i] . name, name)))
        {
          success = TRUE;
          break;
        }
      else
        i ++;
    }

  if (success)
    return (hotkeys [i] . value);
  else
    {
      char buffer [64] = "invalid hotkey ";
      strcat (buffer, name);

      ShowRequester (buffer);
    }

  return -1;
}

