/*
 *   
 *
 * Copyright  1990-2006 Sun Microsystems, Inc. All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version
 * 2 only, as published by the Free Software Foundation. 
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License version 2 for more details (a copy is
 * included at /legal/license.txt). 
 * 
 * You should have received a copy of the GNU General Public License
 * version 2 along with this work; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA 
 * 
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
 * Clara, CA 95054 or visit www.sun.com if you need additional
 * information or have any questions. 
 * 
 * This source file is specific for Qt-based configurations.
 */

#include <stdio.h>

#include <qobject.h>

#include <midpEvents.h>

/**
 * @file
 * System specific methods to support the queuing of of events from
 * native threads other than Java VM thread.
 */

/**
 * A QT specific class to support the queuing of of events from
 * native threads other than Java VM thread.
 */
class EventChannel : public QObject {
    Q_OBJECT

    public:
        EventChannel() {};
        void sendEventToVmThread(MidpEvent, int);
    public slots:
        void receiveEvent(MidpEvent, int);
    signals:
        void sendEvent(MidpEvent, int);
};

/* Generated by the QT MOC compiler from the class def in this file. */
#include <moc_midpEvents_md.cpp>

/** The one and only event channel for use by StoreMIDPEvent. */
static EventChannel* pEventChannel = NULL;

/**
 * Emit a sendEvent Signal with the given event.
 *
 * @param event The event to enqueue.
 *
 * @param isolateId ID of an Isolate or 0 for SMV mode
 */
void EventChannel::sendEventToVmThread(MidpEvent event, int isolateId) {
    emit sendEvent(event, isolateId);
}

/**
 * Receive a sendEvent Signal.
 *
 * @param event The event to enqueue.
 *
 * @param isolateId ID of an Isolate or 0 for SMV mode
 */
void EventChannel::receiveEvent(MidpEvent event, int isolateId) {
    StoreMIDPEventInVmThread(event, isolateId);
}

/**
 * Enqueues an event to be processed by the Java event thread.
 * This can be used by any native thread. If the caller is running
 * in the VM thread it can call StoreMIDPEventInVmThread instead to cut down on
 * event latency.
 *
 * @param event The event to enqueue.
 *
 * @param isolateId ID of an Isolate or 0 for SMV mode
 */
extern "C" void StoreMIDPEvent(MidpEvent event, int isolateId) {
    if (NULL == pEventChannel) {
        pEventChannel = new EventChannel();
        QObject::connect(pEventChannel, SIGNAL(sendEvent(MidpEvent, int)),
                         pEventChannel, SLOT(receiveEvent(MidpEvent, int)));
    }

    pEventChannel->sendEventToVmThread(event, isolateId);
}
