/**********************************************************************/
/*                            CapsLockOn.c                            */
/*        This utility polls the keyboard and returns WARN if         */
/*               Caps Lock key is on, OK if it is not.                */
/**********************************************************************/

#include <exec/types.h>
#include <exec/io.h>
#include <exec/ports.h>
#include <exec/memory.h>
#include <devices/keyboard.h>

#include <proto/exec.h>
#include <stdio.h>

#define MATRIX_SIZE 16L
VOID main(int argc, char **argv)
{
    int rc=0;
    struct IOStdReq *keyRequest;
    struct MsgPort *keyPort;
    UBYTE keyMatrix[MATRIX_SIZE];

    if (keyPort = CreatePort(NULL,NULL))
    {
        if (keyRequest = (struct IOStdReq *)CreateExtIO(keyPort, 
                                    sizeof(struct IOStdReq)))
        {
            if (!OpenDevice("keyboard.device",NULL,
                                (struct IORequest *)keyRequest, NULL))
            {
                keyRequest->io_Command=KBD_READMATRIX;
                keyRequest->io_Data=(APTR)&keyMatrix;
                keyRequest->io_Length = 13;
                DoIO((struct IORequest *)keyRequest);
                CloseDevice((struct IORequest *)keyRequest);
                
                /* rc = 5 if capslock is on, 0 if otherwise */
                if (keyMatrix[12] & 0x0004)
                    rc = 5;
            }
            DeleteExtIO((struct IORequest *)keyRequest);
        }
        DeletePort(keyPort);
    }
    exit(rc);
}
