1 | /** @file
|
---|
2 | * MS COM / XPCOM Abstraction Layer:
|
---|
3 | * Event and EventQueue class declaration
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | *
|
---|
26 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 | #ifndef ___VBox_com_EventQueue_h
|
---|
32 | #define ___VBox_com_EventQueue_h
|
---|
33 |
|
---|
34 | #if !defined (VBOX_WITH_XPCOM)
|
---|
35 | # include <Windows.h>
|
---|
36 | #else
|
---|
37 | # include <nsEventQueueUtils.h>
|
---|
38 | #endif
|
---|
39 |
|
---|
40 | #include <VBox/com/defs.h>
|
---|
41 | #include <VBox/com/assert.h>
|
---|
42 |
|
---|
43 | namespace com
|
---|
44 | {
|
---|
45 |
|
---|
46 | class EventQueue;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Base class for all events. Intended to be subclassed to introduce new events
|
---|
50 | * and handlers for them.
|
---|
51 | *
|
---|
52 | * Subclasses usually reimplement virtual #handler() (that does nothing by
|
---|
53 | * default) and add new data members describing the event.
|
---|
54 | */
|
---|
55 | class Event
|
---|
56 | {
|
---|
57 | public:
|
---|
58 |
|
---|
59 | Event() {}
|
---|
60 |
|
---|
61 | protected:
|
---|
62 |
|
---|
63 | virtual ~Event() {};
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Event handler. Called in the context of the event queue's thread.
|
---|
67 | * Always reimplemented by subclasses
|
---|
68 | *
|
---|
69 | * @return reserved, should be NULL.
|
---|
70 | */
|
---|
71 | virtual void *handler() { return NULL; }
|
---|
72 |
|
---|
73 | friend class EventQueue;
|
---|
74 | };
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Simple event queue.
|
---|
78 | *
|
---|
79 | * When using XPCOM, this will map onto the default XPCOM queue for the thread.
|
---|
80 | * So, if a queue is created on the main thread, it automatically processes
|
---|
81 | * XPCOM/IPC events while waiting for its own (Event) events.
|
---|
82 | *
|
---|
83 | * When using Windows, Darwin and OS/2, this will map onto the native thread
|
---|
84 | * queue/runloop. So, windows messages and what not will be processed while
|
---|
85 | * waiting for events.
|
---|
86 | */
|
---|
87 | class EventQueue
|
---|
88 | {
|
---|
89 | public:
|
---|
90 |
|
---|
91 | EventQueue();
|
---|
92 | ~EventQueue();
|
---|
93 |
|
---|
94 | BOOL postEvent (Event *event);
|
---|
95 | BOOL waitForEvent (Event **event);
|
---|
96 | BOOL handleEvent (Event *event);
|
---|
97 | int processEventQueue(uint32_t cMsTimeout);
|
---|
98 | int interruptEventQueueProcessing();
|
---|
99 | int getSelectFD();
|
---|
100 | static int init();
|
---|
101 | static int uninit();
|
---|
102 | static EventQueue *getMainEventQueue();
|
---|
103 |
|
---|
104 | private:
|
---|
105 | static EventQueue *mMainQueue;
|
---|
106 |
|
---|
107 | #if !defined (VBOX_WITH_XPCOM)
|
---|
108 |
|
---|
109 | /** The thread which the queue belongs to. */
|
---|
110 | DWORD mThreadId;
|
---|
111 | /** Duplicated thread handle for MsgWaitForMultipleObjects. */
|
---|
112 | HANDLE mhThread;
|
---|
113 |
|
---|
114 | #else
|
---|
115 |
|
---|
116 | /** Whether it was created (and thus needs destroying) or if a queue already
|
---|
117 | * associated with the thread was used. */
|
---|
118 | BOOL mEQCreated;
|
---|
119 |
|
---|
120 | nsCOMPtr <nsIEventQueue> mEventQ;
|
---|
121 | nsCOMPtr <nsIEventQueueService> mEventQService;
|
---|
122 |
|
---|
123 | Event *mLastEvent;
|
---|
124 | BOOL mGotEvent;
|
---|
125 |
|
---|
126 | struct MyPLEvent : public PLEvent
|
---|
127 | {
|
---|
128 | MyPLEvent (Event *e) : event (e) {}
|
---|
129 | Event *event;
|
---|
130 | };
|
---|
131 |
|
---|
132 | static void * PR_CALLBACK plEventHandler (PLEvent* self)
|
---|
133 | {
|
---|
134 | // nsIEventQueue doesn't expose PL_GetEventOwner(), so use an internal
|
---|
135 | // field of PLEvent directly (hackish, but doesn' require an extra lib)
|
---|
136 | EventQueue *owner = (EventQueue *) self->owner;
|
---|
137 | Assert (owner);
|
---|
138 | owner->mLastEvent = ((MyPLEvent *) self)->event;
|
---|
139 | owner->mGotEvent = TRUE;
|
---|
140 | return 0;
|
---|
141 | }
|
---|
142 |
|
---|
143 | static void PR_CALLBACK plEventDestructor (PLEvent* self) { delete self; }
|
---|
144 |
|
---|
145 | #endif
|
---|
146 | };
|
---|
147 |
|
---|
148 | } /* namespace com */
|
---|
149 |
|
---|
150 | #endif
|
---|
151 |
|
---|