/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "Licence").
 * You may not use this file except in compliance with the Licence.
 *
 * You can obtain a copy of the licence at RISC OS path @.^.LICENCE
 * or  http://www.riscosdev.com/lanman98/LICENCE.CDDL
 * See the Licence for the specific language governing permissions
 * and limitations under the Licence.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the Licence file. If applicable, add the
 * following below this CDDL HEADER, with the fields enclosed by
 * brackets "[]" replaced with your own identifying information:
 * Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
 
/*
 *   Portions Copyright 1996 Warm Silence Software Ltd.
 */

/******************************************************/
/*                                                    */
/* Name: PGscan.h                                     */
/* Author: Paul Gardiner.                             */
/*                                                    */
/******************************************************/

#include "kernel.h"
#include "swis.h"
#include "PGscan.h"

static _kernel_swi_regs regs;

static int get_one(void)
{
    regs.r[0] = 129;
    regs.r[1] = 0;
    regs.r[2] = 0;
    _kernel_swi(OS_Byte, &regs, &regs);
    if(regs.r[2] == 0)
        return regs.r[1];
    else
        return -1;
}

int ScanMouseS(void)
{
    static int t0, t1;

    _kernel_swi(OS_Mouse, &regs, &regs);
    if((regs.r[2] & 4) != 0)
    {
        t1 = regs.r[3];
        if(t1 - t0 < 20)
            return 2;
        else
            return 1;
    }
    else
    {
        t0 = t1;
        return 0;
    }
}

int ScanMouseA(void)
{
    _kernel_swi(OS_Mouse, &regs, &regs);
    return (regs.r[2] & 1) != 0;
}

int ScanMouseM(void)
{
    _kernel_swi(OS_Mouse, &regs, &regs);
    return (regs.r[2] & 2) != 0;
}

int ScanKey(void)
{
    int c;
    c = get_one();
    if(c == 0)
      return get_one() | 0x100;
    else
      return c;
}
