/*
 * input map code
 *
 * Copyright 2003 Jacob Robbins
 *
 *
 * 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 "../include.h"
#include <stdio.h>

event_input_map_t* event_input_map_create(){
  event_input_map_t* newmap = (event_input_map_t*)malloc(sizeof(event_input_map_t));

  newmap->owner_object = 0;
  newmap->event_type = -1;
  if (!(newmap->submap_array = generic_array_create(sizeof(event_submap_t*)))){
    free(newmap);
    return 0;
  }

  return newmap;
}



void event_input_map_destroy(event_input_map_t* inmap){
  int i;
  event_submap_t** sbmap;

  for (i=0; i < generic_array_get_size(inmap->submap_array); ++i){
    sbmap = (event_submap_t**)generic_array_get_element_pointer(inmap->submap_array,i);
    event_submap_destroy(*sbmap);
  }

  generic_array_destroy(inmap->submap_array);

  free(inmap);
}

void* event_input_map_get_owner_object(event_input_map_t* inmap){
  return inmap->owner_object;
}

void event_input_map_set_owner_object(event_input_map_t* inmap, void* newobject){
  inmap->owner_object = newobject;
}

int event_input_map_get_event_type(event_input_map_t* inmap){
  return inmap->event_type;
}

void event_input_map_set_event_type(event_input_map_t* inmap, int newtype){
  inmap->event_type = newtype;
}

 
int event_input_map_get_submap_array_size(event_input_map_t* inmap){
  return generic_array_get_size(inmap->submap_array);
}

event_submap_t* event_input_map_get_submap(event_input_map_t* inmap, int pos){
  return *(event_submap_t**)generic_array_get_element_pointer(inmap->submap_array,pos);
}


int event_input_map_insert_submap(event_input_map_t* inmap, int pos, char data_type, int test_type){
  int i;
  event_submap_t* sbmap;
  if (!(sbmap =  event_submap_create(data_type, test_type))){return -1;}
  
  if ((i = generic_array_insert_element(inmap->submap_array, pos, (void*)(&sbmap)))<0){
    event_submap_destroy(sbmap);
    return -1;
  }

  return i;
}


int event_input_map_remove_submap(event_input_map_t* inmap, int pos){
  return generic_array_remove_element(inmap->submap_array, pos, 0);
}


int event_input_map_append_submap(event_input_map_t* inmap, char data_type, int test_type){
  int i;
  event_submap_t* sbmap;
  if (!(sbmap =  event_submap_create(data_type, test_type))) return -1;
  
  if ((i = generic_array_append_element(inmap->submap_array, (void*)(&sbmap)))<0){
    event_submap_destroy(sbmap);
    return -1;
  }

  return i;
}


