/******************************************************************************/
/*                                                                            */
/*                 CONTROL SUPPORT [KEYBOARD/JOYSTICK/LEDS]                   */
/*                                                                            */
/******************************************************************************/
/*
  How the controls work :

  the game drivers expect a bit to be cleared or set when a specific control
  changes (like a button pressed/released or a movement of the stick).
  So for each bit of the controls to test, we have the index in our array of
  controls to test.

  For example :
   { KB_DEF_P1_B1,        MSG_P1_B1,               0x000001, 0x10, BIT_ACTIVE_0 },

   means : the key for p1_b1, which is called MSG_P1_B1 in the gui should change
   bitmask 0x10 at offset 1 in the controls so that the bit is 0 if the key is
   pressed.

   For the keyboard, KB_DEF_P1_B1 just gives an index in the allegro key array
   to check.
   For the joysticks, we reproduce the same behaviour by updating our own
   rjoy array based on the joystick changes (see update_rjoy_lists)
*/

#include <SDL.h>
#include "deftypes.h"
#include "games.h"
#include "control.h"
#include "demos.h"
#include "ingame.h"
#include "savegame.h"           // Save/Load game stuff
#include "profile.h" // switch_fps_mode
#include "gui/sound.h"
#include "emumain.h" // key_pause_fwd

volatile int *MouseB;
UINT32 p1_trackball_x;
UINT32 p1_trackball_y;

static int mickey_x, mickey_y;

void my_get_mouse_mickeys(int *mx, int *my) {
  *mx = mickey_x;
  *my = mickey_y;
}

void (*GetMouseMickeys)(int *mx,int *my) = &my_get_mouse_mickeys;

/******************************************************************************/
/*                                                                            */
/*                        DEFAULT GAME KEY SETTINGS                           */
/*                                                                            */
/******************************************************************************/

typedef struct DEF_INPUT
{
   UINT16 scancode;
   UINT32 joycode;
   char *name;
} DEF_INPUT;

/* This is the "direct mapping" of the joystick or digital, that is moved or not moved,
   nothing in the middle. For "analog" mode, we'll add something different (new type of
   input probably). So the weird way to handle joystick axes... */
#define JOY(NUM_JOY, AXE, BUTTON, HAT_NUM) \
  (NUM_JOY) | (AXE<<8) | (BUTTON<<16) | (HAT_NUM<<24)

#define AXE_LEFT(NUM) (NUM*2)
#define AXE_RIGHT(NUM) (NUM*2+1)

   /* Use this way : HAT(1,SDL_HAT_UP)
      the positions are a bit mask in sdl from 0 to 8, it should not change soon,
      so it uses the first 4 bits in this byte. The upper 4 bits are the hat number
      but I don't expect to have so many hats one day ! */
#define HAT(NUM,POS) ((NUM<<4)|pos)

static struct DEF_INPUT def_input_list[KB_DEF_COUNT] =
{
 { SDLK_3,       JOY(1,0,10,0), "Def Coin A",           },      // KB_DEF_COIN1,
 { SDLK_4,       JOY(2,0,10,0),"Def Coin B",           },      // KB_DEF_COIN2,
 { SDLK_7,       JOY(3,0,10,0),"Def Coin C",           },      // KB_DEF_COIN3,
 { SDLK_8,       JOY(4,0,10,0),"Def Coin D",           },      // KB_DEF_COIN4,

 { SDLK_t,       0x00,           "Def Tilt",             },      // KB_DEF_TILT,
 { SDLK_y,       0x00,           "Def Service",          },      // KB_DEF_SERVICE,
 { SDLK_u,       0x00,           "Def Test",             },      // KB_DEF_TEST,

 { SDLK_1,       JOY(1,0,9,0), "Def P1 Start",         },      // KB_DEF_P1_START,

 { SDLK_UP,      JOY(1,AXE_RIGHT(1),0,0),      "Def P1 Up",            },      // KB_DEF_P1_UP,
 { SDLK_DOWN,    JOY(1,AXE_LEFT(1),0,0),    "Def P1 Down",          },      // KB_DEF_P1_DOWN,
 { SDLK_LEFT,    JOY(1,AXE_LEFT(0),0,0),    "Def P1 Left",          },      // KB_DEF_P1_LEFT,
 { SDLK_RIGHT,   JOY(1,AXE_RIGHT(0),0,0),   "Def P1 Right",         },      // KB_DEF_P1_RIGHT,

 { SDLK_v,       JOY(1,0,1,0), "Def P1 Button 1",      },      // KB_DEF_P1_B1,
 { SDLK_b,       JOY(1,0,2,0), "Def P1 Button 2",      },      // KB_DEF_P1_B2,
 { SDLK_n,       JOY(1,0,3,0), "Def P1 Button 3",      },      // KB_DEF_P1_B3,
 { SDLK_g,       JOY(1,0,4,0), "Def P1 Button 4",      },      // KB_DEF_P1_B4,
 { SDLK_h,       JOY(1,0,5,0), "Def P1 Button 5",      },      // KB_DEF_P1_B5,
 { SDLK_j,       JOY(1,0,6,0), "Def P1 Button 6",      },      // KB_DEF_P1_B6,
 { SDLK_m,       JOY(1,0,7,0), "Def P1 Button 7",      },      // KB_DEF_P1_B7,
 { SDLK_k,       JOY(1,0,8,0), "Def P1 Button 8",      },      // KB_DEF_P1_B8,

 { SDLK_2,       JOY(2,0,9,0), "Def P2 Start",         },      // KB_DEF_P2_START,

 { SDLK_s,       JOY(2,AXE_RIGHT(1),0,0),      "Def P2 Up",            },      // KB_DEF_P2_UP,
 { SDLK_x,       JOY(2,AXE_LEFT(1),0,0),    "Def P2 Down",          },      // KB_DEF_P2_DOWN,
 { SDLK_z,       JOY(2,AXE_LEFT(0),0,0),    "Def P2 Left",          },      // KB_DEF_P2_LEFT,
 { SDLK_c,       JOY(2,AXE_RIGHT(0),0,0),   "Def P2 Right",         },      // KB_DEF_P2_RIGHT,

 { SDLK_q,       JOY(2,0,1,0), "Def P2 Button 1",      },      // KB_DEF_P2_B1,
 { SDLK_w,       JOY(2,0,2,0), "Def P2 Button 2",      },      // KB_DEF_P2_B2,
 { SDLK_e,       JOY(2,0,3,0), "Def P2 Button 3",      },      // KB_DEF_P2_B3,
 { SDLK_r,       JOY(2,0,4,0), "Def P2 Button 4",      },      // KB_DEF_P2_B4,
 { SDLK_a,       JOY(2,0,5,0), "Def P2 Button 5",      },      // KB_DEF_P2_B5,
 { SDLK_d,       JOY(2,0,6,0), "Def P2 Button 6",      },      // KB_DEF_P2_B6,
 { SDLK_f,       JOY(2,0,7,0), "Def P2 Button 7",      },      // KB_DEF_P2_B7,
 { SDLK_g,       JOY(2,0,8,0), "Def P2 Button 8",      },      // KB_DEF_P2_B8,

 { SDLK_5,       JOY(3,0,9,0), "Def P3 Start",         },      // KB_DEF_P3_START,

 { 0,       JOY(3,AXE_RIGHT(1),0,0),      "Def P3 Up",            },      // KB_DEF_P2_UP,
 { 0,       JOY(3,AXE_LEFT(1),0,0),    "Def P3 Down",          },      // KB_DEF_P2_DOWN,
 { 0,       JOY(3,AXE_LEFT(0),0,0),    "Def P3 Left",          },      // KB_DEF_P2_LEFT,
 { 0,       JOY(3,AXE_RIGHT(0),0,0),   "Def P3 Right",         },      // KB_DEF_P2_RIGHT,

 { 0x00,        JOY(3,0,1,0), "Def P3 Button 1",      },      // KB_DEF_P3_B1,
 { 0x00,        JOY(3,0,2,0), "Def P3 Button 2",      },      // KB_DEF_P3_B2,
 { 0x00,        JOY(3,0,3,0), "Def P3 Button 3",      },      // KB_DEF_P3_B3,
 { 0x00,        JOY(3,0,4,0), "Def P3 Button 4",      },      // KB_DEF_P3_B4,
 { 0x00,        JOY(3,0,5,0), "Def P3 Button 5",      },      // KB_DEF_P3_B5,
 { 0x00,        JOY(3,0,6,0), "Def P3 Button 6",      },      // KB_DEF_P3_B6,
 { 0x00,        JOY(3,0,7,0), "Def P3 Button 7",      },      // KB_DEF_P3_B7,
 { 0x00,        JOY(3,0,8,0), "Def P3 Button 8",      },      // KB_DEF_P3_B8,

 { SDLK_6,       JOY(4,0,9,0), "Def P4 Start",         },      // KB_DEF_P4_START,

 { 0,       JOY(4,AXE_RIGHT(1),0,0),      "Def P4 Up",            },      // KB_DEF_P2_UP,
 { 0,       JOY(4,AXE_LEFT(1),0,0),    "Def P4 Down",          },      // KB_DEF_P2_DOWN,
 { 0,       JOY(4,AXE_LEFT(0),0,0),    "Def P4 Left",          },      // KB_DEF_P2_LEFT,
 { 0,       JOY(4,AXE_RIGHT(0),0,0),   "Def P4 Right",         },      // KB_DEF_P2_RIGHT,

 { 0x00,        JOY(4,0,1,0), "Def P4 Button 1",      },      // KB_DEF_P4_B1,
 { 0x00,        JOY(4,0,2,0), "Def P4 Button 2",      },      // KB_DEF_P4_B2,
 { 0x00,        JOY(4,0,3,0), "Def P4 Button 3",      },      // KB_DEF_P4_B3,
 { 0x00,        JOY(4,0,4,0), "Def P4 Button 4",      },      // KB_DEF_P4_B4,
 { 0x00,        JOY(4,0,5,0), "Def P4 Button 5",      },      // KB_DEF_P4_B5,
 { 0x00,        JOY(4,0,6,0), "Def P4 Button 6",      },      // KB_DEF_P4_B6,
 { 0x00,        JOY(4,0,7,0), "Def P4 Button 7",      },      // KB_DEF_P4_B7,
 { 0x00,        JOY(4,0,8,0), "Def P4 Button 8",      },      // KB_DEF_P4_B8,

 { SDLK_LCTRL,0x00,           "Def Flipper 1 Left",   },      // KB_DEF_FLIPPER_1_L,
 { SDLK_RCTRL,0x00,           "Def Flipper 1 Right",  },      // KB_DEF_FLIPPER_1_R,
 { SDLK_LSHIFT,  0x00,           "Def Flipper 2 Left",   },      // KB_DEF_FLIPPER_2_L,
 { SDLK_RSHIFT,  0x00,           "Def Flipper 2 Right",  },      // KB_DEF_FLIPPER_2_R,
 { SDLK_BACKSLASH,0x00,         "Def Tilt Left",        },      // KB_DEF_TILT_L,
 { SDLK_SLASH,   0x00,           "Def Tilt Right",       },      // KB_DEF_TILT_R,
 { SDLK_z,       0x00,           "Def Button 1 Left",    },      // KB_DEF_B1_L,
 { SDLK_e,    0x00,           "Def Button 1 Right",   },      // KB_DEF_B1_R,

 // Mahjong controls, at least in mahjong quest...
 { SDLK_a,       0x00,           "Def P1 A",             }, // KB_DEF_P1_A
 { SDLK_e,       0x00,           "Def P1 E",             }, // KB_DEF_P1_E
 { SDLK_i,       0x00,           "Def P1 I",             }, // KB_DEF_P1_I
 { SDLK_m,       0x00,           "Def P1 M",             }, // KB_DEF_P1_M
 { SDLK_LCTRL,0x00,           "Def P1 Kan",           }, // KB_DEF_P1_KAN
 { SDLK_b,       0x00,           "Def P1 B",             }, // KB_DEF_P1_B
 { SDLK_f,       0x00,           "Def P1 F",             }, // KB_DEF_P1_F
 { SDLK_j,       0x00,           "Def P1 J",             }, // KB_DEF_P1_J
 { SDLK_n,       0x00,           "Def P1 N",             }, // KB_DEF_P1_N
 { SDLK_LSHIFT,  0x00,           "Def P1 Reach",         }, // KB_DEF_P1_REACH
 { SDLK_c,       0x00,           "Def P1 C",             }, // KB_DEF_P1_C
 { SDLK_g,       0x00,           "Def P1 G",             }, // KB_DEF_P1_G
 { SDLK_k,       0x00,           "Def P1 K",             }, // KB_DEF_P1_K
 { SDLK_SPACE,   0x00,           "Def P1 Chi",           }, // KB_DEF_P1_CHI
 { SDLK_z,       0x00,           "Def P1 Ron",           }, // KB_DEF_P1_RON,
 { SDLK_d,       0x00,           "Def P1 D",             }, // KB_DEF_P1_D
 { SDLK_h,       0x00,           "Def P1 H",             }, // KB_DEF_P1_H
 { SDLK_l,       0x00,           "Def P1 L",             }, // KB_DEF_P1_L
 { SDLK_LALT,     0x00,           "Def P1 Pon",           }, // KB_DEF_P1_PON

 { 0,           0,              "Def Service A", }, // KB_DEF_SERVICE_A
 { 0,           0,              "Def Service B", }, // KB_DEF_SERVICE_B
 { 0,           0,              "Def Service C", }, // KB_DEF_SERVICE_C

 { SDLK_r,       0x00,           "Def Button 2 Left",    },      // KB_DEF_B2_L,
 { SDLK_t,    0x00,           "Def Button 2 Right",   },      // KB_DEF_B2_R,

};

/******************************************************************************/
/*                                                                            */
/*                       DEFAULT EMULATOR KEY SETTINGS                        */
/*                                                                            */
/******************************************************************************/

static struct DEF_INPUT def_input_list_emu[KB_EMU_DEF_COUNT] =
{
 { SDLK_0,       0x00,           "Save Screenshot",      },      // KB_EMU_SCREENSHOT,
 { SDLK_PAGEUP,    0x00,           "Increase frameskip",   },      // KB_EMU_INC_FRAMESKIP,
 { SDLK_PAGEDOWN,    0x00,           "Decrease frameskip",   },      // KB_EMU_DEC_FRAMESKIP,
 { SDLK_HOME,    0x00,           "Increase cpu skip",    },      // KB_EMU_INC_CPU_FRAMESKIP,
 { SDLK_END,     0x00,           "Decrease cpu skip",    },      // KB_EMU_DEC_CPU_FRAMESKIP,
 { SDLK_F2,      0x00,           "Save game",            },      // KB_EMU_SAVE_GAME,
 { SDLK_F3,      0x00,           "Switch save slot",     },      // KB_EMU_SWITCH_SAVE_SLOT,
 { SDLK_F4,      0x00,           "Load game",            },      // KB_EMU_LOAD_GAME,
 { SDLK_F11,     0x00,           "Switch fps display",   },      // KB_EMU_SWITCH_FPS,
 { SDLK_F1,      0x00,           "Reset game",           },      // KB_EMU_RESET_GAME,
 { SDLK_p,       0x00,           "Pause game",           },      // KB_EMU_PAUSE_GAME,
 { SDLK_ESCAPE,     0x00,           "Stop emulation",       },      // KB_EMU_STOP_EMULATION,
 { SDLK_TAB,     0x00,           "Return to gui",        },      // KB_EMU_OPEN_GUI,
#if 0
 { SDLK_UP | (KB_CTRL_FLAG<<8),      0,      "Screen up",            },      // KB_EMU_SCREEN_UP,
 { SDLK_DOWN | (KB_CTRL_FLAG<<8),    0,    "Screen down",          },      // KB_EMU_SCREEN_DOWN,
 { SDLK_LEFT | (KB_CTRL_FLAG<<8),    0,    "Screen left",          },      // KB_EMU_SCREEN_LEFT,
 { SDLK_RIGHT | (KB_CTRL_FLAG<<8),   0,   "Screen right",         },      // KB_EMU_SCREEN_RIGHT,
#endif
 { SDLK_WORLD_18,   0x00,           "Switch Mixer",         },      // Mixer
 { SDLK_F2 | (KB_CTRL_FLAG<<8), 0x00, "Save game with name",  },      // KB_EMU_SAVE_GAME,
 { SDLK_F4 | (KB_CTRL_FLAG<<8), 0x00, "Load game with name",  },      // KB_EMU_LOAD_GAME,
 { SDLK_F2 | (KB_SHIFT_FLAG<<8), 0x00, "Save demo",  },      // KB_EMU_SAVE_DEMO,
 { SDLK_F4 | (KB_SHIFT_FLAG<<8), 0x00, "Load demo",  },      // KB_EMU_LOAD_DEMO,
 { SDLK_SPACE, 0x00, "Fwd 1 frame in pause", },
};

/* Active inputs (allows to switch from default to custom inputs/game) */
typedef struct INPUT
{
   UINT16 Key;			// keyboard code for this input
   UINT32 Joy;			// joystick code for this input
   UINT8 *InputName;		// input name string
   UINT32 Address;		// address in RAM[] for this input
   UINT8 Bit;			// bit mask for this input
   UINT8 high_bit;		// input is acitve high or active low
   UINT16 default_key;		// index in default controls array (to retrieve defaults)
   UINT8 auto_rate;		// autofire frame rate
   UINT32 active_time;		// frames since the input became active (used for coin time)
} INPUT;

#define MAX_INPUTS 64

static struct INPUT InputList[MAX_INPUTS];	// Max 64 control inputs in a game

static int InputCount;			// Number of Inputs in InputList


static void set_key_from_default(INPUT *inp)
{

  if(!(inp->default_key&0x0100))

    inp->Key = inp->default_key & 0xFF;

  else

    inp->Key = def_input_list[inp->default_key & 0xFF].scancode;

}

static void set_joy_from_default(INPUT *inp)
{

  if((!(inp->default_key&0x0100)))

    inp->Joy = 0;

  else {
    inp->Joy = def_input_list[inp->default_key & 0xFF].joycode;
  }

}

void init_inputs(void)
{
   const INPUT_INFO *input_src;

   InputCount = 0;

   input_src = current_game->input_list;

   if(input_src){

     while(input_src[InputCount].name){

       InputList[InputCount].default_key = input_src[InputCount].default_key;
       InputList[InputCount].InputName   = input_src[InputCount].name;
       InputList[InputCount].Address     = input_src[InputCount].offset;
       InputList[InputCount].Bit         = input_src[InputCount].bit_mask;
       InputList[InputCount].high_bit    = input_src[InputCount].flags;
       InputList[InputCount].auto_rate   = 0;
       InputList[InputCount].active_time = 0;

       set_key_from_default(&InputList[InputCount]);
       set_joy_from_default(&InputList[InputCount]);

       InputCount++;
     }

   }

}

void reset_game_keys(void)
{
   int ta;

   for(ta=0;ta<InputCount;ta++)
      set_key_from_default(&InputList[ta]);
}

void joy_reset_game_keys(void)
{
   int ta;

   for(ta=0;ta<InputCount;ta++)
      set_joy_from_default(&InputList[ta]);
}

void no_spaces(char *str)
{
   int ta,tb;

   tb=strlen(str);

   for(ta=0;ta<tb;ta++){
      if(((str[ta]<'A')||(str[ta]>'Z'))&&
         ((str[ta]<'a')||(str[ta]>'z'))&&
         ((str[ta]<'0')||(str[ta]>'9'))){
         str[ta]='_';
      }
   }
}

void load_game_keys(char *section)
{
   int ta;
   char key_name[64];

   use_custom_keys = raine_get_config_int(section,"use_custom_keys",0);

   // load keys if using custom keys

   if(use_custom_keys){

      for(ta=0;ta<InputCount;ta++){
         sprintf(key_name,"%s",InputList[ta].InputName);
         no_spaces(key_name);
         InputList[ta].Key = raine_get_config_int(section,key_name,InputList[ta].Key);
      }

   }
}

void save_game_keys(char *section)
{
   int ta;
   char key_name[64];

   // clear the old settings first (keep the file tidy)

   raine_clear_config_section(section);

   // save keys if using custom keys

   if(use_custom_keys){

      raine_set_config_int(section,"use_custom_keys",use_custom_keys);

      for(ta=0;ta<InputCount;ta++){
         sprintf(key_name,"%s",InputList[ta].InputName);
         no_spaces(key_name);
         raine_set_config_int(section,key_name,InputList[ta].Key);
      }

   }
}

void load_game_joys(char *section)
{
   int ta;
   char joy_name[64];

   joy_use_custom_keys = raine_get_config_int(section,"use_custom_joystick",0);

   // load joys if using custom joys

   if(joy_use_custom_keys){

      for(ta=0;ta<InputCount;ta++){
         sprintf(joy_name,"%s",InputList[ta].InputName);
         no_spaces(joy_name);
         InputList[ta].Joy = raine_get_config_int(section,joy_name,InputList[ta].Joy);
      }

   }
}

void save_game_joys(char *section)
{
   int ta;
   char joy_name[64];

   // clear the old settings first (keep the file tidy)

   raine_clear_config_section(section);

   // save joys if using custom joys

   if(joy_use_custom_keys){

      raine_set_config_int(section,"use_custom_joystick",joy_use_custom_keys);

      for(ta=0;ta<InputCount;ta++){
         sprintf(joy_name,"%s",InputList[ta].InputName);
         no_spaces(joy_name);
         raine_set_config_int(section,joy_name,InputList[ta].Joy);
      }

   }
}

void load_default_keys(char *section)
{
   int ta;
   char key_name[64];

   use_custom_keys = 0;

   for(ta=0;ta<KB_DEF_COUNT;ta++){
      sprintf(key_name,"%s",def_input_list[ta].name);
      no_spaces(key_name);
      def_input_list[ta].scancode = raine_get_config_int(section,key_name,def_input_list[ta].scancode);
   }
}

void save_default_keys(char *section)
{
   int ta;
   char key_name[64];

   for(ta=0;ta<KB_DEF_COUNT;ta++){
      sprintf(key_name,"%s",def_input_list[ta].name);
      no_spaces(key_name);
      raine_set_config_int(section,key_name,def_input_list[ta].scancode);
   }
}

void load_default_joys(char *section)
{
   int ta;
   char joy_name[64];

   joy_use_custom_keys = 0;

   for(ta=0;ta<KB_DEF_COUNT;ta++){
      sprintf(joy_name,"%s",def_input_list[ta].name);
      no_spaces(joy_name);
      def_input_list[ta].joycode = raine_get_config_int(section,joy_name,def_input_list[ta].joycode);
   }
}

void save_default_joys(char *section)
{
   int ta;
   char joy_name[64];

   for(ta=0;ta<KB_DEF_COUNT;ta++){
      sprintf(joy_name,"%s",def_input_list[ta].name);
      no_spaces(joy_name);
      raine_set_config_int(section,joy_name,def_input_list[ta].joycode);
   }
}

void load_emulator_keys(char *section)
{
   int ta,scan;
   char key_name[64];

   for(ta=0;ta<KB_EMU_DEF_COUNT;ta++){
      sprintf(key_name,"%s",def_input_list_emu[ta].name);
      no_spaces(key_name);
      scan = raine_get_config_int(section,key_name,def_input_list_emu[ta].scancode);
      if (strncmp(key_name,"Screen_",7) || scan < SDLK_LEFT || scan > SDLK_DOWN) {
	// Forces modifiers for scrolling keys (previously in pause only)
	def_input_list_emu[ta].scancode = scan;
      }
   }
}

void save_emulator_keys(char *section)
{
   int ta;
   char key_name[64];

   for(ta=0;ta<KB_EMU_DEF_COUNT;ta++){
      sprintf(key_name,"%s",def_input_list_emu[ta].name);
      no_spaces(key_name);
      raine_set_config_int(section,key_name,def_input_list_emu[ta].scancode);
   }
}

void load_emulator_joys(char *section)
{
   int ta;
   char joy_name[64];

   for(ta=0;ta<KB_EMU_DEF_COUNT;ta++){
      sprintf(joy_name,"%s",def_input_list_emu[ta].name);
      no_spaces(joy_name);
      def_input_list_emu[ta].joycode = raine_get_config_int(section,joy_name,def_input_list_emu[ta].joycode);
   }
}

void save_emulator_joys(char *section)
{
   int ta;
   char joy_name[64];

   for(ta=0;ta<KB_EMU_DEF_COUNT;ta++){
      sprintf(joy_name,"%s",def_input_list_emu[ta].name);
      no_spaces(joy_name);
      raine_set_config_int(section,joy_name,def_input_list_emu[ta].joycode);
   }
}

void update_rjoy_list(void)
{
}

static int pulse_time;

void set_pulse_time(int time)
{
  pulse_time = time;
}

static int find_input_from_keysym(int sym) {
  int ta;
  for (ta=0; ta<InputCount; ta++) {
    if (InputList[ta].Key == sym) {
      return ta;
    }
  }


  return -1;
}

static UINT8  stick_logic[4];
static UINT8 autofire_timer[6];

static int is_input_valid(int ta) {
  int    input_valid;

  // Increment active timer
  InputList[ta].active_time ++;

  // Assume Input is valid

  input_valid=1;

  // Toggle Autofire settings

  if(InputList[ta].auto_rate){
    if(autofire_timer[InputList[ta].auto_rate] >= InputList[ta].auto_rate) input_valid=0;
  }

  // Disable the following situations:
  // 1) Impossible joystick inputs (joystick can be up or down, but not both)
  // 2) Coin inputs must last approx 250ms (prevent taito coin error)

  switch(InputList[ta].default_key){
#if 0
  case KB_DEF_P1_UP:
    stick_logic[0] |= 0x01;
    if((stick_logic[0]&0x02)) input_valid=0;
    break;
  case KB_DEF_P1_DOWN:
    stick_logic[0] |= 0x02;
    if((stick_logic[0]&0x01)) input_valid=0;
    break;
  case KB_DEF_P1_LEFT:
    stick_logic[0] |= 0x04;
    if((stick_logic[0]&0x08)) input_valid=0;
    break;
  case KB_DEF_P1_RIGHT:
    stick_logic[0] |= 0x08;
    if((stick_logic[0]&0x04)) input_valid=0;
    break;
  case KB_DEF_P2_UP:
    stick_logic[1] |= 0x01;
    if((stick_logic[1]&0x02)) input_valid=0;
    break;
  case KB_DEF_P2_DOWN:
    stick_logic[1] |= 0x02;
    if((stick_logic[1]&0x01)) input_valid=0;
    break;
  case KB_DEF_P2_LEFT:
    stick_logic[1] |= 0x04;
    if((stick_logic[1]&0x08)) input_valid=0;
    break;
  case KB_DEF_P2_RIGHT:
    stick_logic[1] |= 0x08;
    if((stick_logic[1]&0x04)) input_valid=0;
    break;
  case KB_DEF_P3_UP:
    stick_logic[2] |= 0x01;
    if((stick_logic[2]&0x02)) input_valid=0;
    break;
  case KB_DEF_P3_DOWN:
    stick_logic[2] |= 0x02;
    if((stick_logic[2]&0x01)) input_valid=0;
    break;
  case KB_DEF_P3_LEFT:
    stick_logic[2] |= 0x04;
    if((stick_logic[2]&0x08)) input_valid=0;
    break;
  case KB_DEF_P3_RIGHT:
    stick_logic[2] |= 0x08;
    if((stick_logic[2]&0x04)) input_valid=0;
    break;
  case KB_DEF_P4_UP:
    stick_logic[3] |= 0x01;
    if((stick_logic[3]&0x02)) input_valid=0;
    break;
  case KB_DEF_P4_DOWN:
    stick_logic[3] |= 0x02;
    if((stick_logic[3]&0x01)) input_valid=0;
    break;
  case KB_DEF_P4_LEFT:
    stick_logic[3] |= 0x04;
    if((stick_logic[3]&0x08)) input_valid=0;
    break;
  case KB_DEF_P4_RIGHT:
    stick_logic[3] |= 0x08;
    if((stick_logic[3]&0x04)) input_valid=0;
    break;
#endif
  case KB_DEF_COIN1:
  case KB_DEF_COIN2:
  case KB_DEF_COIN3:
  case KB_DEF_COIN4:
    if(InputList[ta].active_time > pulse_time) input_valid=0;
    break;
  default:
    break;
  }
  return input_valid;
}

static void key_up(int ta) {
#if 0
  switch(InputList[ta].default_key){
  case KB_DEF_P1_UP:
    stick_logic[0] &= ~0x01;
    break;
  case KB_DEF_P1_DOWN:
    stick_logic[0] &= ~0x02;
    break;
  case KB_DEF_P1_LEFT:
    stick_logic[0] &= ~0x04;
    break;
  case KB_DEF_P1_RIGHT:
    stick_logic[0] &= ~0x08;
    break;
  case KB_DEF_P2_UP:
    stick_logic[1] &= ~0x01;
    break;
  case KB_DEF_P2_DOWN:
    stick_logic[1] &= ~0x02;
    break;
  case KB_DEF_P2_LEFT:
    stick_logic[1] &= ~0x04;
    break;
  case KB_DEF_P2_RIGHT:
    stick_logic[1] &= ~0x08;
    break;
  case KB_DEF_P3_UP:
    stick_logic[2] &= ~0x01;
    break;
  case KB_DEF_P3_DOWN:
    stick_logic[2] &= ~0x02;
    break;
  case KB_DEF_P3_LEFT:
    stick_logic[2] &= ~0x04;
    break;
  case KB_DEF_P3_RIGHT:
    stick_logic[2] &= ~0x08;
    break;
  case KB_DEF_P4_UP:
    stick_logic[3] &= ~0x01;
    break;
  case KB_DEF_P4_DOWN:
    stick_logic[3] &= ~0x02;
    break;
  case KB_DEF_P4_LEFT:
    stick_logic[3] &= ~0x04;
    break;
  case KB_DEF_P4_RIGHT:
    stick_logic[3] &= ~0x08;
    break;
  }
#endif
}

static void update_input_buffer(int ta, int input_valid) {
  UINT8 *buffer;

  if(InputList[ta].Address < 0x100)
     buffer = input_buffer;
   else
     buffer = RAM;

  buffer += InputList[ta].Address;

  if(input_valid ^ InputList[ta].high_bit)
    *buffer &= ~ InputList[ta].Bit;
   else
     *buffer |=   InputList[ta].Bit;
}

// update_inputs():
// Goes through the input list setting/clearing the mapped RAM[] bits

void update_inputs(void)
{
   SDL_Event event;

   int    ta;
   int    input_valid;

   if (reading_demo) {
     write_demo_inputs();
     return;
   }

   // Autofire timer emulation

   for(ta=0;ta<6;ta++){
      autofire_timer[ta] ++;
      if(autofire_timer[ta] >= (ta<<1))
         autofire_timer[ta] = 0;
   }

   while (SDL_PollEvent(&event)) {
     switch (event.type) {
     case SDL_KEYDOWN:
       ta = find_input_from_keysym(event.key.keysym.sym);
       if (ta >= 0) {
	 input_valid = is_input_valid(ta);
	 update_input_buffer(ta,input_valid);
       }
       // printf("key press %d stick logic %d\n",ta,stick_logic[0]);
       break;
     case SDL_KEYUP:
       ta = find_input_from_keysym(event.key.keysym.sym);
       if (ta >= 0) {
	 key_up(ta);
	 update_input_buffer(ta,0);
       }
       // printf("key up %d stick logic %d\n",ta,stick_logic[0]);
       break;
     }
   }
   if (recording_demo)
     save_demo_inputs();
}

/******************************************************************************/
/*                                                                            */
/*                              GLOBAL STRINGS                                */
/*                                                                            */
/******************************************************************************/

char MSG_COIN1[]        = "Coin A";
char MSG_COIN2[]        = "Coin B";
char MSG_COIN3[]        = "Coin C";
char MSG_COIN4[]        = "Coin D";

char MSG_TILT[]         = "Tilt";
char MSG_SERVICE[]      = "Service";
char MSG_TEST[]         = "Test";
char MSG_UNKNOWN[]      = "Unknown";
char MSG_YES[] = "Yes";
char MSG_NO[] = "No";
char MSG_FREE_PLAY[] = "Free Play";
char MSG_UNUSED[] = "Unused";
char MSG_COINAGE[] = "Coinage";

char MSG_P1_START[]     = "Player1 Start";

char MSG_P1_UP[]        = "Player1 Up";
char MSG_P1_DOWN[]      = "Player1 Down";
char MSG_P1_LEFT[]      = "Player1 Left";
char MSG_P1_RIGHT[]     = "Player1 Right";

char MSG_P1_B1[]        = "Player1 Button1";
char MSG_P1_B2[]        = "Player1 Button2";
char MSG_P1_B3[]        = "Player1 Button3";
char MSG_P1_B4[]        = "Player1 Button4";
char MSG_P1_B5[]        = "Player1 Button5";
char MSG_P1_B6[]        = "Player1 Button6";
char MSG_P1_B7[]        = "Player1 Button7";
char MSG_P1_B8[]        = "Player1 Button8";

char MSG_P2_START[]     = "Player2 Start";

char MSG_P2_UP[]        = "Player2 Up";
char MSG_P2_DOWN[]      = "Player2 Down";
char MSG_P2_LEFT[]      = "Player2 Left";
char MSG_P2_RIGHT[]     = "Player2 Right";

char MSG_P2_B1[]        = "Player2 Button1";
char MSG_P2_B2[]        = "Player2 Button2";
char MSG_P2_B3[]        = "Player2 Button3";
char MSG_P2_B4[]        = "Player2 Button4";
char MSG_P2_B5[]        = "Player2 Button5";
char MSG_P2_B6[]        = "Player2 Button6";
char MSG_P2_B7[]        = "Player2 Button7";
char MSG_P2_B8[]        = "Player2 Button8";

char MSG_P3_START[]     = "Player3 Start";

char MSG_P3_UP[]        = "Player3 Up";
char MSG_P3_DOWN[]      = "Player3 Down";
char MSG_P3_LEFT[]      = "Player3 Left";
char MSG_P3_RIGHT[]     = "Player3 Right";

char MSG_P3_B1[]        = "Player3 Button1";
char MSG_P3_B2[]        = "Player3 Button2";
char MSG_P3_B3[]        = "Player3 Button3";
char MSG_P3_B4[]        = "Player3 Button4";
char MSG_P3_B5[]        = "Player3 Button5";
char MSG_P3_B6[]        = "Player3 Button6";
char MSG_P3_B7[]        = "Player3 Button7";
char MSG_P3_B8[]        = "Player3 Button8";

char MSG_P4_START[]     = "Player4 Start";

char MSG_P4_UP[]        = "Player4 Up";
char MSG_P4_DOWN[]      = "Player4 Down";
char MSG_P4_LEFT[]      = "Player4 Left";
char MSG_P4_RIGHT[]     = "Player4 Right";

char MSG_P4_B1[]        = "Player4 Button1";
char MSG_P4_B2[]        = "Player4 Button2";
char MSG_P4_B3[]        = "Player4 Button3";
char MSG_P4_B4[]        = "Player4 Button4";
char MSG_P4_B5[]        = "Player4 Button5";
char MSG_P4_B6[]        = "Player4 Button6";
char MSG_P4_B7[]        = "Player4 Button7";
char MSG_P4_B8[]        = "Player4 Button8";

char MSG_FLIPPER_1_L[]  = "Flipper 1 Left";
char MSG_FLIPPER_1_R[]  = "Flipper 1 Right";
char MSG_FLIPPER_2_L[]  = "Flipper 2 Left";
char MSG_FLIPPER_2_R[]  = "Flipper 2 Right";
char MSG_TILT_L[]       = "Tilt Left";
char MSG_TILT_R[]       = "Tilt Right";
char MSG_B1_L[]         = "Button 1 Left";
char MSG_B1_R[]         = "Button 1 Right";
char MSG_B2_L[]         = "Button 2 Left";
char MSG_B2_R[]         = "Button 2 Right";

char MSG_P1_A[]         = "P1 A";    // Mahjong controls[]; at least in mahjong quest...
char MSG_P1_E[]         = "P1 E";
char MSG_P1_I[]         = "P1 I";
char MSG_P1_M[]         = "P1 M";
char MSG_P1_KAN[]       = "P1 Kan";

char MSG_P1_B[]         = "P1 B";
char MSG_P1_F[]         = "P1 F";
char MSG_P1_J[]         = "P1 J";
char MSG_P1_N[]         = "P1 N";
char MSG_P1_REACH[]     = "P1 Reach";

char MSG_P1_C[]         = "P1 C";
char MSG_P1_G[]         = "P1 G";
char MSG_P1_K[]         = "P1 K";
char MSG_P1_CHI[]       = "P1 Chi";
char MSG_P1_RON[]       = "P1 Ron";

char MSG_P1_D[]         = "P1 D";
char MSG_P1_H[]         = "P1 H";
char MSG_P1_L[]         = "P1 L";
char MSG_P1_PON[]       = "P1 Pon";

char *MSG_P1_SERVICE_A  = "Service A";
char *MSG_P1_SERVICE_B  = "Service B";
char *MSG_P1_SERVICE_C  = "Service C";

/* DSW SECTION */

char MSG_DSWA_BIT1[]    = "DSW A Bit 1";        // Since most dsw info sheets
char MSG_DSWA_BIT2[]    = "DSW A Bit 2";        // number the bits 1-8, we will
char MSG_DSWA_BIT3[]    = "DSW A Bit 3";        // too, although 0-7 is a more
char MSG_DSWA_BIT4[]    = "DSW A Bit 4";        // correct syntax for progammers.
char MSG_DSWA_BIT5[]    = "DSW A Bit 5";
char MSG_DSWA_BIT6[]    = "DSW A Bit 6";
char MSG_DSWA_BIT7[]    = "DSW A Bit 7";
char MSG_DSWA_BIT8[]    = "DSW A Bit 8";

char MSG_DSWB_BIT1[]    = "DSW B Bit 1";
char MSG_DSWB_BIT2[]    = "DSW B Bit 2";
char MSG_DSWB_BIT3[]    = "DSW B Bit 3";
char MSG_DSWB_BIT4[]    = "DSW B Bit 4";
char MSG_DSWB_BIT5[]    = "DSW B Bit 5";
char MSG_DSWB_BIT6[]    = "DSW B Bit 6";
char MSG_DSWB_BIT7[]    = "DSW B Bit 7";
char MSG_DSWB_BIT8[]    = "DSW B Bit 8";

char MSG_DSWC_BIT1[]    = "DSW C Bit 1";
char MSG_DSWC_BIT2[]    = "DSW C Bit 2";
char MSG_DSWC_BIT3[]    = "DSW C Bit 3";
char MSG_DSWC_BIT4[]    = "DSW C Bit 4";
char MSG_DSWC_BIT5[]    = "DSW C Bit 5";
char MSG_DSWC_BIT6[]    = "DSW C Bit 6";
char MSG_DSWC_BIT7[]    = "DSW C Bit 7";
char MSG_DSWC_BIT8[]    = "DSW C Bit 8";

char MSG_COIN_SLOTS[]   = "Coin Slots";

char MSG_1COIN_1PLAY[]  = "1 Coin/1 Credit";
char MSG_1COIN_2PLAY[]  = "1 Coin/2 Credits";
char MSG_1COIN_3PLAY[]  = "1 Coin/3 Credits";
char MSG_1COIN_4PLAY[]  = "1 Coin/4 Credits";
char MSG_1COIN_5PLAY[]  = "1 Coin/5 Credits";
char MSG_1COIN_6PLAY[]  = "1 Coin/6 Credits";
char MSG_1COIN_7PLAY[]  = "1 Coin/7 Credits";
char MSG_1COIN_8PLAY[]  = "1 Coin/8 Credits";
char MSG_1COIN_9PLAY[]  = "1 Coin/9 Credits";

char MSG_2COIN_1PLAY[]  = "2 Coins/1 Credit";
char MSG_2COIN_2PLAY[]  = "2 Coins/2 Credits";
char MSG_2COIN_3PLAY[]  = "2 Coins/3 Credits";
char MSG_2COIN_4PLAY[]  = "2 Coins/4 Credits";
char MSG_2COIN_5PLAY[]  = "2 Coins/5 Credits";
char MSG_2COIN_6PLAY[]  = "2 Coins/6 Credits";
char MSG_2COIN_7PLAY[]  = "2 Coins/7 Credits";
char MSG_2COIN_8PLAY[]  = "2 Coins/8 Credits";

char MSG_3COIN_1PLAY[]  = "3 Coins/1 Credit";
char MSG_3COIN_2PLAY[]  = "3 Coins/2 Credits";
char MSG_3COIN_3PLAY[]  = "3 Coins/3 Credits";
char MSG_3COIN_4PLAY[]  = "3 Coins/4 Credits";
char MSG_3COIN_5PLAY[]  = "3 Coins/5 Credits";
char MSG_3COIN_6PLAY[]  = "3 Coins/6 Credits";
char MSG_3COIN_7PLAY[]  = "3 Coins/7 Credits";
char MSG_3COIN_8PLAY[]  = "3 Coins/8 Credits";

char MSG_4COIN_1PLAY[]  = "4 Coins/1 Credit";
char MSG_4COIN_2PLAY[]  = "4 Coins/2 Credits";
char MSG_4COIN_3PLAY[]  = "4 Coins/3 Credits";
char MSG_4COIN_4PLAY[]  = "4 Coins/4 Credits";
char MSG_4COIN_5PLAY[]  = "4 Coins/5 Credits";
char MSG_4COIN_6PLAY[]  = "4 Coins/6 Credits";
char MSG_4COIN_7PLAY[]  = "4 Coins/7 Credits";
char MSG_4COIN_8PLAY[]  = "4 Coins/8 Credits";

char MSG_5COIN_1PLAY[]  = "5 Coins/1 Credit";
char MSG_5COIN_2PLAY[]  = "5 Coins/2 Credits";
char MSG_5COIN_3PLAY[]  = "5 Coins/3 Credits";
char MSG_5COIN_4PLAY[]  = "5 Coins/4 Credits";

char MSG_6COIN_1PLAY[]  = "6 Coins/1 Credit";
char MSG_6COIN_2PLAY[]  = "6 Coins/2 Credits";
char MSG_6COIN_3PLAY[]  = "6 Coins/3 Credits";
char MSG_6COIN_4PLAY[]  = "6 Coins/4 Credits";

char MSG_7COIN_1PLAY[]  = "7 Coins/1 Credit";
char MSG_7COIN_2PLAY[]  = "7 Coins/2 Credits";
char MSG_7COIN_3PLAY[]  = "7 Coins/3 Credits";
char MSG_7COIN_4PLAY[]  = "7 Coins/4 Credits";

char MSG_8COIN_1PLAY[]  = "8 Coins/1 Credit";
char MSG_8COIN_2PLAY[]  = "8 Coins/2 Credits";
char MSG_8COIN_3PLAY[]  = "8 Coins/3 Credits";
char MSG_8COIN_4PLAY[]  = "8 Coins/4 Credits";

char MSG_9COIN_1PLAY[] = "9 Coins/1 Credit";

char MSG_OFF[]          = "Off";
char MSG_ON[]           = "On";

char MSG_SCREEN[]       = "Flip Screen";
char MSG_NORMAL[]       = "Normal";
char MSG_INVERT[]       = "Invert";

char MSG_TEST_MODE[]    = "Test Mode";

char MSG_DEMO_SOUND[]   = "Demo Sound";

char MSG_CONTINUE_PLAY[]= "Continue Play";
char MSG_EXTRA_LIFE[]   = "Extra Life";
char MSG_LIVES[]        = "Lives";

char MSG_CHEAT[]        = "Cheat";

char MSG_DIFFICULTY[]   = "Difficulty";
char MSG_EASY[]         = "Easy";
char MSG_HARD[]         = "Hard";
char MSG_HARDEST[]      = "Hardest";
char MSG_MEDIUM[]       = "Medium";

char MSG_CABINET[]      = "Cabinet";
char MSG_UPRIGHT[]      = "Upright";
char MSG_TABLE[]        = "Table";
char MSG_ALT[]        = "Alternate";

/******************************************************************************/
/*                                                                            */
/*                          KEYBOARD LED SUPPORT                              */
/*                                                                            */
/******************************************************************************/

// status of leds and old status (internal)

static UINT32 leds,oldleds;

#if 0
static UINT32 led_data;
#endif

// switch led: call this to turn a led on/off

void switch_led(int num, int on)
{
   static const UINT32 led_flags[3] =
   {
      KB_NUMLOCK_FLAG,
      KB_CAPSLOCK_FLAG,
      KB_SCROLOCK_FLAG
   };

   // Update Keyboard LEDS (support only 3)

   if(num<3){

      if(on&1)

	 leds |=  (led_flags[num]);

      else

	 leds &= ~(led_flags[num]);

   }
}

// update leds: call this once a frame

void update_leds(void)
{
   if(use_leds){

   if(leds!=oldleds){
      oldleds=leds;
      set_leds(leds);
   }

   }
}

// turn off all leds: call this when resetting game

void init_leds(void)
{
   if(use_leds){

      leds = 0;
      oldleds = 1;

   }
}

// forced update leds: call this when going back into game

void force_update_leds(void)
{
   if(use_leds)

      set_leds(leds);

}

/******************************************************************************/
static void frame_skip_up(void)
{
   if((display_cfg.frame_skip<9) && (display_cfg.frame_skip)){
      display_cfg.frame_skip++;
      print_ingame(120,"Drawing Every %1d Frames",display_cfg.frame_skip);
   }
}

static void frame_skip_down(void)
{
   if((display_cfg.frame_skip>1) && (display_cfg.frame_skip)){
      display_cfg.frame_skip--;
      if(display_cfg.frame_skip==1)
	 print_ingame(120,"Drawing All Frames");
      else
	 print_ingame(120,"Drawing Every %1d Frames",display_cfg.frame_skip);
   }
}

static void key_save_screen(void)
{
   raine_cfg.req_save_screen = 1;
}

static void key_pause_game(void)
{
   raine_cfg.req_pause_game = 1;
}

static void key_unpause_game(void)
{
   raine_cfg.req_pause_game = 0;
}

static void key_pause_scroll_up(void)
{
   raine_cfg.req_pause_scroll |= 1;
}

static void key_pause_scroll_down(void)
{
   raine_cfg.req_pause_scroll |= 2;
}

static void key_pause_scroll_left(void)
{
   raine_cfg.req_pause_scroll |= 4;
}

static void key_pause_scroll_right(void)
{
   raine_cfg.req_pause_scroll |= 8;
}

typedef struct CORE_CTRL
{
   UINT16 key;
   void  (*proc)();
   UINT16 status; // The status is usefull to make the difference between
   // keys with only 1 action when you press them
   // and keys which work until you unpress them (key & 0x400)
} CORE_CTRL;

/*

ingame emulator controls

*/

extern void key_stop_emulation_esc(void);
extern void key_stop_emulation_tab(void);
extern void cpu_speed_up();
extern void cpu_slow_down();

static struct CORE_CTRL core_ctrl_list[] =
{
   { KB_EMU_SCREENSHOT,        key_save_screen,        },
   { KB_EMU_INC_FRAMESKIP,     frame_skip_up,	       },
   { KB_EMU_DEC_FRAMESKIP,     frame_skip_down,        },
   { KB_EMU_INC_CPU_FRAMESKIP, cpu_speed_up,	       },
   { KB_EMU_DEC_CPU_FRAMESKIP, cpu_slow_down,	       },
   { KB_EMU_SAVE_GAME,	       GameSave,	       },
   { KB_EMU_SAVE_GAME_NAME,    GameSaveName,	       },
   { KB_EMU_SAVE_DEMO,	       GameSaveDemo,	       },
   { KB_EMU_SWITCH_SAVE_SLOT,  next_save_slot,	       },
   { KB_EMU_LOAD_GAME,	       GameLoad,	       },
   { KB_EMU_LOAD_GAME_NAME,    GameLoadName,	       },
   { KB_EMU_LOAD_DEMO,	       GameLoadDemo,	       },
   { KB_EMU_SWITCH_FPS,        switch_fps_mode,        },
   { KB_EMU_RESET_GAME,        reset_game_hardware,    },
   { KB_EMU_PAUSE_GAME,        key_pause_game,	       },
   { KB_EMU_STOP_EMULATION,    key_stop_emulation_esc, },
   { KB_EMU_OPEN_GUI,	       key_stop_emulation_tab, },
   { KB_EMU_SWITCH_MIXER,      switch_mixer, },
#if 0
   { KB_EMU_SCREEN_UP	 | 0x4000, key_pause_scroll_up,    },
   { KB_EMU_SCREEN_DOWN  | 0x4000, key_pause_scroll_down,  },
   { KB_EMU_SCREEN_LEFT  | 0x4000, key_pause_scroll_left,  },
   { KB_EMU_SCREEN_RIGHT | 0x4000, key_pause_scroll_right, },
#endif
   { KB_EMU_REQ_FWD, key_pause_fwd, },
   { 0, 		       NULL,		       },
};

/*

pause mode emulator controls

*/
void update_gui_inputs_paused(void);

void init_gui_inputs(void)
{
  // pause key : normal mapping
  int ta;
  CORE_CTRL *ctrl_list = core_ctrl_list;
   while(ctrl_list->proc){

      if(ctrl_list->key == KB_EMU_PAUSE_GAME)

	ctrl_list->proc = &key_pause_game;
      ctrl_list++;
   }

   stick_logic[0]=0x00;
   stick_logic[1]=0x00;
   stick_logic[2]=0x00;
   stick_logic[3]=0x00;

   for (ta=0; ta<InputCount; ta++)
     update_input_buffer(ta,0);
}


void init_gui_inputs_paused(void)
{
  // pause key : unpause
  CORE_CTRL *ctrl_list = core_ctrl_list;
   while(ctrl_list->proc){

      if(ctrl_list->key == KB_EMU_PAUSE_GAME) {

	ctrl_list->proc = &key_unpause_game;
      }
      ctrl_list++;

   }
}


static void parse_control_list(CORE_CTRL *ctrl_list)
{
  // This function parses the CORE_CTRL list, which is the emu inputs.
  // Contrary to the normal game inputs, this function takes care about
  // modifiers (ctrl, shift, alt, left & right win, and menu).
  DEF_INPUT *inp;
   while(ctrl_list->proc){
      inp = &def_input_list_emu[ctrl_list->key & 0xFF];
      if(ctrl_list->key & 0x4000){ // ignore status code

	 if((key[inp->scancode & 0xff] && ((key_shifts & 0x3f) == inp->scancode>>8))
	   /* || (rjoy[inp->joycode]) */){
	       ctrl_list->proc();
	 }

      }
      else{

	 if((key[inp->scancode & 0xff] && ((key_shifts & 0x3f) == inp->scancode>>8))
	   /* || (rjoy[inp->joycode]) */){
	    if(!ctrl_list->status){
	       ctrl_list->status = 1;
	       ctrl_list->proc();
	    }
	 }
	 else{
	    ctrl_list->status = 0;
	 }

      }

      ctrl_list++;

   }

}


void update_gui_inputs(void)
{
   parse_control_list(core_ctrl_list);

   check_layer_switches();
}

