home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Club Amiga de Montreal - CAM
/
CAM_CD_1.iso
/
files
/
581b.lha
/
GetKeyMap
/
GetKeyMap.c
next >
Wrap
C/C++ Source or Header
|
1991-12-01
|
4KB
|
193 lines
/* include files */
#include <intuition/intuition.h>
#include <devices/keymap.h>
#include <devices/console.h>
#include <string.h>
struct IntuitionBase * GKM_IntuitionBase;
#define INT_REV 0
/* These ports are neccessary to open the console device to get the current
keymap */
struct MsgPort * CreatePort();
struct IOStdReq * CreateStdIO();
void DeletePort();
void DeleteStdIO();
short openerror;
struct MsgPort * ConPort;
struct IOStdReq * ConMsg;
/* The keymap that the console will return is not changeable. I tried.
First we get the keymap that the console returns and then copy it into
the keymap structure that you passed to the GetKeyMap function. This
structure with hold the one that the console returns. */
struct KeyMap nochange_keymap;
/* Following is a clean exit routine. */
void GKM_Close_All()
{
if(!openerror) CloseDevice(ConMsg);
if(ConMsg) DeleteStdIO(ConMsg);
if(ConPort) DeletePort(ConPort);
if(GKM_IntuitionBase) CloseLibrary(GKM_IntuitionBase);
}
/* Here we open the libraries, port and message. */
BOOL GKM_Open_All()
{
if((GKM_IntuitionBase = (struct IntuitionBase *)
OpenLibrary("intuition.library", INT_REV)) == NULL)
{
GKM_Close_All();
return(FALSE);
}
if(!(ConPort = CreatePort(0L, 0L)))
{
GKM_Close_All();
return(FALSE);
}
if(!(ConMsg = CreateStdIO(ConPort)))
{
GKM_Close_All();
return(FALSE);
}
return(TRUE);
}
/* This function sends a command to the console device. And checks for
errors. */
BOOL DoIt(UWORD cmd, ULONG len, APTR ptr)
{
BYTE Error;
ConMsg->io_Command = cmd;
ConMsg->io_Length = len;
ConMsg->io_Data = ptr;
DoIO(ConMsg);
Error = ConMsg->io_Error;
if(Error)
{
GKM_Close_All();
return(FALSE);
}
return(TRUE);
}
/* The console device needs a window to receive and send data through. Here
is a small window off of the workbench. */
struct Window * GKM_Window()
{
struct NewWindow nw;
nw.LeftEdge = 0;
nw.TopEdge = 0;
nw.Width = 10;
nw.Height = 10;
nw.DetailPen = 1;
nw.BlockPen = 1;
nw.Flags = NULL;
nw.IDCMPFlags = NULL;
nw.FirstGadget = NULL;
nw.CheckMark = NULL;
nw.Title = NULL;
nw.Screen = NULL;
nw.BitMap = NULL;
nw.MinWidth = 0;
nw.MinHeight = 0;
nw.MaxWidth = 0;
nw.MaxHeight = 0;
nw.Type = WBENCHSCREEN;
return((struct Window *) OpenWindow(&nw));
}
/* This is the actual function. */
BOOL GetKeyMap(struct KeyMap * your_keymap)
{
/* Structure pointer for our small window. */
struct Window * Wnd;
/* Open everthing. */
if(!GKM_Open_All()) return(FALSE);
/* Open our window. You will see that it does not stay open any longer
than the time that we need it. */
if((Wnd = GKM_Window()) == NULL)
{
GKM_Close_All();
return(FALSE);
}
/* Assign the window pointer to the IO message for the console open. */
ConMsg->io_Data = (APTR)Wnd;
/* Open the console device and check for errors. */
if(openerror = OpenDevice("console.device", 0L, ConMsg, 0L))
{
GKM_Close_All();
return(FALSE);
}
/* Ask the console device to return the current keymap in the
non-changeable structure. */
if(!DoIt(CD_ASKKEYMAP, sizeof(struct KeyMap), (APTR)&nochange_keymap))
return(FALSE);
/* Copy the non-changeable structure into the structure that was supplied
to the function. */
memcpy((char *)your_keymap->km_LoKeyMapTypes,
(char *) nochange_keymap.km_LoKeyMapTypes,
sizeof(UBYTE) * 0x40);
memcpy((char *)your_keymap->km_LoKeyMap,
(char *) nochange_keymap.km_LoKeyMap,
sizeof(ULONG) * 0x40);
memcpy((char *)your_keymap->km_LoCapsable,
(char *) nochange_keymap.km_LoCapsable,
sizeof(UBYTE) * 8);
memcpy((char *)your_keymap->km_LoRepeatable,
(char *) nochange_keymap.km_LoRepeatable,
sizeof(UBYTE) * 8);
memcpy((char *)your_keymap->km_HiKeyMapTypes,
(char *) nochange_keymap.km_HiKeyMapTypes,
sizeof(UBYTE) * 0x40);
memcpy((char *)your_keymap->km_HiKeyMap,
(char *) nochange_keymap.km_HiKeyMap,
sizeof(ULONG) * 0x40);
memcpy((char *)your_keymap->km_HiCapsable,
(char *) nochange_keymap.km_HiCapsable,
sizeof(UBYTE) * 8);
memcpy((char *)your_keymap->km_HiRepeatable,
(char *) nochange_keymap.km_HiRepeatable,
sizeof(UBYTE) * 8);
/* Here we end the short life-span of our little window and close up shop.
Non of the openned libraries or console device has to remain open. */
CloseWindow(Wnd);
GKM_Close_All();
return(TRUE);
}