#include "wiimote.h"


Wiimote::Wiimote(MythInputManagerInterface * inputManager, cwiid_bdinfo infoWiimote, int remoteId)
        :QThread(NULL),
        m_inputManager(inputManager),
        m_remoteId(remoteId)

{
    m_infoWiimote=infoWiimote;
    char macAddr[18];
    ba2str(&infoWiimote.bdaddr, macAddr);
    m_inputManager->logInfo(QString("Wiimote #%1 connected").arg(m_remoteId));
}
Wiimote::~Wiimote()
{
    m_inputManager->logInfo(QString("Wiimote #%1 disconnected").arg(m_remoteId));
}


void Wiimote::run()
{
	char macAddr[18];
    ba2str(&m_infoWiimote.bdaddr, macAddr);
    m_inputManager->logInfo(QString("MythWiimote: try opening wii remote. %1 [%2]")
            .arg(m_infoWiimote.name)
            .arg(macAddr)); 
    cwiid_wiimote_t *m_wiimote = cwiid_open(&m_infoWiimote.bdaddr, CWIID_FLAG_MESG_IFC);//|CWIID_FLAG_CONTINUOUS);
    if (m_wiimote!= NULL)
    {
        cwiid_set_rpt_mode (m_wiimote, CWIID_RPT_STATUS | CWIID_RPT_BTN); 
        m_inputManager->logInfo(QString("MythWiimote: wii remote opened, start reading"));
        int mesg_count;
        union cwiid_mesg *mesg;
        struct timespec timestamp;
    	cwiid_set_led(m_wiimote, m_remoteId);
        bool disconnected=false;
        while (!disconnected)
        {
            cwiid_get_mesg(m_wiimote, &mesg_count, &mesg, &timestamp);
            int iMesg;

            for (iMesg=0; iMesg < mesg_count; iMesg++)
            {
                switch (mesg[iMesg].type)
                {
                    case CWIID_MESG_STATUS:
                    {
                        struct cwiid_status_mesg * status_mesg = 
                            (struct cwiid_status_mesg *) &mesg[iMesg];
                        m_inputManager->logInfo(QString("wiimote STATUS battery=%1")
                            .arg(status_mesg->battery));
                        break;
                    }
                    case CWIID_MESG_BTN:
                        {
                        struct cwiid_btn_mesg * status_mesg = 
                                (struct cwiid_btn_mesg *) &mesg[iMesg];
                        m_inputManager->logInfo(QString("STATUS buttons=%1")
                                .arg(status_mesg->buttons));
                        QString keyStr;
                        if (status_mesg->buttons & CWIID_BTN_LEFT)
                            keyStr="Left";
                        if (status_mesg->buttons & CWIID_BTN_RIGHT)
                            keyStr="Right";
                        if (status_mesg->buttons & CWIID_BTN_UP)
                            keyStr="Up";
                        if (status_mesg->buttons & CWIID_BTN_DOWN)
                            keyStr="Down";
                        if (status_mesg->buttons & CWIID_BTN_A)
                            keyStr="Enter";
                        if (status_mesg->buttons & CWIID_BTN_B)
                            keyStr="Escape";
                        if (!keyStr.isEmpty())
                        {
                            QKeySequence keyseq(keyStr);
                            if (keyseq.count()>0)
                                m_inputManager->onKeyPressed(
                                QKeyEvent(QEvent::KeyPress, keyseq[0],Qt::NoModifier));
                        }
                        break;
                        }
                    case CWIID_MESG_ERROR:
                        disconnected=true;
                        break;
                    default:break;
                }
            }
        }
        cwiid_close(m_wiimote);
    }
    emit onDisconnected(this);
}
