/****************************************************************************
**
** Copyright (C) 2000-$THISYEAR$ $TROLLTECH$. All rights reserved.
**
** This file is part of the $MODULE$ of the Qtopia Toolkit.
**
** $TROLLTECH_DUAL_LICENSE$
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#include "brcm1103kbdhandler.h"
#include <QScreen>
#include <QSocketNotifier>

#include "qscreen_qws.h"
#include "qwindowsystem_qws.h"
#include "qapplication.h"
#include "qnamespace.h"

#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <sys/vt.h>
#include <sys/kd.h>


Brcm1103KbdHandler::Brcm1103KbdHandler()
{
    setObjectName( "Brcm1103 Keypad Handler" );

    //kbdFD = ::open("/dev/keypad0", O_RDONLY | O_NDELAY, 0);
    kbdFD = ::open("/dev/tty0", O_RDONLY | O_NDELAY, 0);
    if (kbdFD >= 0)
    {
        m_notify = new QSocketNotifier(kbdFD, QSocketNotifier::Read, this);
        connect(m_notify, SIGNAL(activated(int)), this, SLOT(readKbdData()));

        struct termios termdata;
        tcgetattr(kbdFD, &termdata);
#if defined(Q_OS_LINUX)
# ifdef QT_QWS_USE_KEYCODES
        ioctl(kbdFD, KDSKBMODE, K_MEDIUMRAW);
# else
        ioctl(kbdFD, KDSKBMODE, K_RAW);
# endif
#endif
        termdata.c_iflag = (IGNPAR | IGNBRK) & (~PARMRK) & (~ISTRIP);
        termdata.c_oflag = 0;
        termdata.c_cflag = CREAD | CS8;
        termdata.c_lflag = 0;
        termdata.c_cc[VTIME]=0;
        termdata.c_cc[VMIN]=1;
        cfsetispeed(&termdata, EXTB);//9600);
        cfsetospeed(&termdata, EXTB);//9600);
        tcsetattr(kbdFD, TCSANOW, &termdata);
    }
    else
    {
        // qWarning("Cannot open keypad0 for keypad (%s)", strerror(errno));
        qWarning("Cannot open tty0 for keypad (%s)", strerror(errno));
        return;
    }

}

Brcm1103KbdHandler::~Brcm1103KbdHandler()
{
    if (kbdFD >= 0)
    {
        ::close(kbdFD);
    }
}


void Brcm1103KbdHandler::readKbdData()
{
    unsigned char           buf[40];
    unsigned short          driverKeyCode;
    unsigned short          unicode;
    unsigned int            keyCode;
    bool                    isPressed;
    Qt::KeyboardModifiers   modifiers = Qt::NoModifier;

    int n = ::read(kbdFD, buf, 40);
    if ( n < 0 ) {
	printf("keypad read error\n");
        if ( errno != EINTR && errno != EAGAIN )
            return;
    }

    for (int loop = 0; loop < n; loop++)
    {
        driverKeyCode   = (unsigned short)buf[loop];
        keyCode         = driverKeyCode & 0x7f;
        unicode         = 0xffff;
        isPressed       = (driverKeyCode & 0x80) == 0;

	//printf("driver:  key: %i %s\n", keyCode, (isPressed) ? "true" : "false");
        processKeyEvent(unicode, keyCode, modifiers, isPressed, false);
/*
        if (isPressed)
            beginAutoRepeat(unicode, keyCode, modifiers);
        else
            endAutoRepeat();
*/
    }
}

