/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id: $
 *
 * Copyright (C) 2002 Philipp Pertermann
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied. 
 *
 ****************************************************************************/
#ifndef __KEY_SCHEME_H__
#define __KEY_SCHEME_H__

/**
 * Describes a command that can be executed by a button event.
 * All available commands are stored in key_command_table
 */
struct key_command {
    void (*action)(void);          /* NULL if type != ACTION */
    int  (*get_value)(void);       /* NULL if type != VALUE */
    void (*set_value)(int value);  /* NULL if type != VALUE */
    void (*get_text)(void);        /* NULL if type != TEXT */
    void (*set_text)(char *value); /* NULL if type != TEXT */
};

/* The CMD_XXX constants are for use with key_map.cmd_use */
#define CMD_ACTION     0
#define CMD_VALUE_SET  1
#define CMD_VALUE_INC  2
#define CMD_VALUE_DEC  3
#define CMD_TEXT_SET   4
#define CMD_TEXT_EDIT  5

/* describes which command to execute when button_event occurred */
struct key_map {
    int button_event;    /* the event that triggers the command */
    int cmd_use;         /* specifies how the command shall be executed */
    int value;           /* used if type is CMD_SET */
    int next_key_scheme; /* next key scheme to use */
    char *text;          /* used if type is CMD_TEXT_SET */
    struct key_command *command;  /* the command to execute */
};

/** 
 * A key scheme is a collection of key_map objects that 
 * are stored in key_scheme_store. The store_XXX incices
 * describe the range within the key_map_store.
 */
struct key_scheme {
    int parent;        /* inherit from parent scheme with this id */
    int start;         /* index for key_map_store */
    int end;           /* index for key_map_store */
};

/* contains all available key_commands */
#define KEY_COMMAND_TABLE_SIZE 0xff
extern struct key_command key_command_table[KEY_COMMAND_TABLE_SIZE];

/**
 * Stores all key_maps of all key schemes.
 * Which key_maps belong to which key scheme is stored
 * in key_schemes
 */
#define KEY_MAP_STORE_SIZE 0xFF
extern struct key_map key_map_store[KEY_MAP_STORE_SIZE];

/**
 * all available key schemes
 */
#define KEY_SCHEME_STORE_SIZE 0x20
extern struct key_scheme key_scheme_store[KEY_SCHEME_STORE_SIZE];

#define SCHEME_EMPTY   -1
#define SCHEME_DEFAULT  0

/** 
 * should always contain the key scheme that currently is in use 
 * The value is an index for key_scheme_store.
 */
extern int key_scheme_current;

/**
 * Use the scheme and execute the command that is mapped 
 * to the given button event.
 * @param int button - A value as received from button_get
 * @param int scheme - A valid index for key_scheme_store
 * @return int - The index of the key scheme that must be
 * used after the execution of the command. If no command
 * was assigned to the button scheme is returned.
 */
extern int select_and_execute(int button, int scheme);

/**
 * load schemes from a text file
 */
extern void load_scheme(char *filename);

#endif /* __KEY_SCHEME_H__ */

