EventQueue.h revision 10a17670b96a3e6ddaa9ffe524882f600bd41259
7568150a58e78021968b6c22bc28e9787b33496agwr/** @file
7568150a58e78021968b6c22bc28e9787b33496agwr * MS COM / XPCOM Abstraction Layer:
7568150a58e78021968b6c22bc28e9787b33496agwr * Event and EventQueue class declaration
7568150a58e78021968b6c22bc28e9787b33496agwr */
7568150a58e78021968b6c22bc28e9787b33496agwr
7568150a58e78021968b6c22bc28e9787b33496agwr/*
7568150a58e78021968b6c22bc28e9787b33496agwr * Copyright (C) 2006-2007 innotek GmbH
7568150a58e78021968b6c22bc28e9787b33496agwr *
7568150a58e78021968b6c22bc28e9787b33496agwr * This file is part of VirtualBox Open Source Edition (OSE), as
7568150a58e78021968b6c22bc28e9787b33496agwr * available from http://www.virtualbox.org. This file is free software;
7568150a58e78021968b6c22bc28e9787b33496agwr * you can redistribute it and/or modify it under the terms of the GNU
7568150a58e78021968b6c22bc28e9787b33496agwr * General Public License as published by the Free Software Foundation,
7568150a58e78021968b6c22bc28e9787b33496agwr * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
7568150a58e78021968b6c22bc28e9787b33496agwr * distribution. VirtualBox OSE is distributed in the hope that it will
7568150a58e78021968b6c22bc28e9787b33496agwr * be useful, but WITHOUT ANY WARRANTY of any kind.
7568150a58e78021968b6c22bc28e9787b33496agwr */
7568150a58e78021968b6c22bc28e9787b33496agwr
7568150a58e78021968b6c22bc28e9787b33496agwr#ifndef ___VBox_com_EventQueue_h
7568150a58e78021968b6c22bc28e9787b33496agwr#define ___VBox_com_EventQueue_h
7568150a58e78021968b6c22bc28e9787b33496agwr
7568150a58e78021968b6c22bc28e9787b33496agwr#if defined (RT_OS_WINDOWS)
7568150a58e78021968b6c22bc28e9787b33496agwr#include <windows.h>
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross#else
7568150a58e78021968b6c22bc28e9787b33496agwr#include <nsEventQueueUtils.h>
7568150a58e78021968b6c22bc28e9787b33496agwr#endif
7568150a58e78021968b6c22bc28e9787b33496agwr
7568150a58e78021968b6c22bc28e9787b33496agwr#include <VBox/com/defs.h>
7568150a58e78021968b6c22bc28e9787b33496agwr#include <VBox/com/assert.h>
7568150a58e78021968b6c22bc28e9787b33496agwr
7568150a58e78021968b6c22bc28e9787b33496agwrnamespace com
7568150a58e78021968b6c22bc28e9787b33496agwr{
7568150a58e78021968b6c22bc28e9787b33496agwr
7568150a58e78021968b6c22bc28e9787b33496agwrclass EventQueue;
7568150a58e78021968b6c22bc28e9787b33496agwr
7568150a58e78021968b6c22bc28e9787b33496agwr/**
7568150a58e78021968b6c22bc28e9787b33496agwr * Base class for all events. Intended to be subclassed to introduce new events
7568150a58e78021968b6c22bc28e9787b33496agwr * and handlers for them.
7568150a58e78021968b6c22bc28e9787b33496agwr *
7568150a58e78021968b6c22bc28e9787b33496agwr * Subclasses usually reimplement virtual #handler() (that does nothing by
7568150a58e78021968b6c22bc28e9787b33496agwr * default) and add new data members describing the event.
7568150a58e78021968b6c22bc28e9787b33496agwr */
7568150a58e78021968b6c22bc28e9787b33496agwrclass Event
7568150a58e78021968b6c22bc28e9787b33496agwr{
7568150a58e78021968b6c22bc28e9787b33496agwrpublic:
7568150a58e78021968b6c22bc28e9787b33496agwr
7568150a58e78021968b6c22bc28e9787b33496agwr Event() {}
7568150a58e78021968b6c22bc28e9787b33496agwr
7568150a58e78021968b6c22bc28e9787b33496agwrprotected:
7568150a58e78021968b6c22bc28e9787b33496agwr
7568150a58e78021968b6c22bc28e9787b33496agwr virtual ~Event() {};
7568150a58e78021968b6c22bc28e9787b33496agwr
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross /**
7568150a58e78021968b6c22bc28e9787b33496agwr * Event handler. Called in the context of the event queue's thread.
7568150a58e78021968b6c22bc28e9787b33496agwr * Always reimplemented by subclasses
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross *
613a2f6ba31e891e3d947a356daf5e563d43c1ceGordon Ross * @return reserved, should be NULL.
9c9af2590af49bb395bc8d2eace0f2d4ea16d165Gordon Ross */
7568150a58e78021968b6c22bc28e9787b33496agwr virtual void *handler() { return NULL; }
7568150a58e78021968b6c22bc28e9787b33496agwr
7568150a58e78021968b6c22bc28e9787b33496agwr friend class EventQueue;
7568150a58e78021968b6c22bc28e9787b33496agwr};
7568150a58e78021968b6c22bc28e9787b33496agwr
7568150a58e78021968b6c22bc28e9787b33496agwr/**
7568150a58e78021968b6c22bc28e9787b33496agwr * Simple event queue.
7568150a58e78021968b6c22bc28e9787b33496agwr *
7568150a58e78021968b6c22bc28e9787b33496agwr * On Linux, if this queue is created on the main thread, it automatically
7568150a58e78021968b6c22bc28e9787b33496agwr * processes XPCOM/IPC events while waiting for its own (Event) events.
7568150a58e78021968b6c22bc28e9787b33496agwr */
7568150a58e78021968b6c22bc28e9787b33496agwrclass EventQueue
7568150a58e78021968b6c22bc28e9787b33496agwr{
7568150a58e78021968b6c22bc28e9787b33496agwrpublic:
7568150a58e78021968b6c22bc28e9787b33496agwr
7568150a58e78021968b6c22bc28e9787b33496agwr EventQueue();
7568150a58e78021968b6c22bc28e9787b33496agwr ~EventQueue();
7568150a58e78021968b6c22bc28e9787b33496agwr
7568150a58e78021968b6c22bc28e9787b33496agwr BOOL postEvent (Event *event);
7568150a58e78021968b6c22bc28e9787b33496agwr BOOL waitForEvent (Event **event);
7568150a58e78021968b6c22bc28e9787b33496agwr BOOL handleEvent (Event *event);
7568150a58e78021968b6c22bc28e9787b33496agwr
7568150a58e78021968b6c22bc28e9787b33496agwrprivate:
7568150a58e78021968b6c22bc28e9787b33496agwr
7568150a58e78021968b6c22bc28e9787b33496agwr#if defined (RT_OS_WINDOWS)
7568150a58e78021968b6c22bc28e9787b33496agwr
7568150a58e78021968b6c22bc28e9787b33496agwr DWORD mThreadId;
7568150a58e78021968b6c22bc28e9787b33496agwr
7568150a58e78021968b6c22bc28e9787b33496agwr#else
7568150a58e78021968b6c22bc28e9787b33496agwr
7568150a58e78021968b6c22bc28e9787b33496agwr BOOL mEQCreated;
7568150a58e78021968b6c22bc28e9787b33496agwr
7568150a58e78021968b6c22bc28e9787b33496agwr nsCOMPtr <nsIEventQueue> mEventQ;
7568150a58e78021968b6c22bc28e9787b33496agwr nsCOMPtr <nsIEventQueueService> mEventQService;
7568150a58e78021968b6c22bc28e9787b33496agwr
7568150a58e78021968b6c22bc28e9787b33496agwr Event *mLastEvent;
7568150a58e78021968b6c22bc28e9787b33496agwr BOOL mGotEvent;
7568150a58e78021968b6c22bc28e9787b33496agwr
7568150a58e78021968b6c22bc28e9787b33496agwr struct MyPLEvent : public PLEvent
7568150a58e78021968b6c22bc28e9787b33496agwr {
7568150a58e78021968b6c22bc28e9787b33496agwr MyPLEvent (Event *e) : event (e) {}
7568150a58e78021968b6c22bc28e9787b33496agwr Event *event;
7568150a58e78021968b6c22bc28e9787b33496agwr };
7568150a58e78021968b6c22bc28e9787b33496agwr
7568150a58e78021968b6c22bc28e9787b33496agwr static void * PR_CALLBACK plEventHandler (PLEvent* self)
7568150a58e78021968b6c22bc28e9787b33496agwr {
7568150a58e78021968b6c22bc28e9787b33496agwr // nsIEventQueue doesn't expose PL_GetEventOwner(), so use an internal
7568150a58e78021968b6c22bc28e9787b33496agwr // field of PLEvent directly (hackish, but doesn' require an extra lib)
7568150a58e78021968b6c22bc28e9787b33496agwr EventQueue *owner = (EventQueue *) self->owner;
7568150a58e78021968b6c22bc28e9787b33496agwr Assert (owner);
7568150a58e78021968b6c22bc28e9787b33496agwr owner->mLastEvent = ((MyPLEvent *) self)->event;
7568150a58e78021968b6c22bc28e9787b33496agwr owner->mGotEvent = TRUE;
7568150a58e78021968b6c22bc28e9787b33496agwr return 0;
7568150a58e78021968b6c22bc28e9787b33496agwr }
7568150a58e78021968b6c22bc28e9787b33496agwr
7568150a58e78021968b6c22bc28e9787b33496agwr static void PR_CALLBACK plEventDestructor (PLEvent* self) { delete self; }
7568150a58e78021968b6c22bc28e9787b33496agwr
7568150a58e78021968b6c22bc28e9787b33496agwr#endif
7568150a58e78021968b6c22bc28e9787b33496agwr};
7568150a58e78021968b6c22bc28e9787b33496agwr
7568150a58e78021968b6c22bc28e9787b33496agwr} /* namespace com */
7568150a58e78021968b6c22bc28e9787b33496agwr
7568150a58e78021968b6c22bc28e9787b33496agwr#endif
7568150a58e78021968b6c22bc28e9787b33496agwr
7568150a58e78021968b6c22bc28e9787b33496agwr