1 | /** @file
|
---|
2 | * MS COM / XPCOM Abstraction Layer:
|
---|
3 | * Event and EventQueue class declaration
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #ifndef ___VBox_com_EventQueue_h
|
---|
19 | #define ___VBox_com_EventQueue_h
|
---|
20 |
|
---|
21 | #if defined (RT_OS_WINDOWS)
|
---|
22 | #include <windows.h>
|
---|
23 | #else
|
---|
24 | #include <nsEventQueueUtils.h>
|
---|
25 | #endif
|
---|
26 |
|
---|
27 | #include <VBox/com/defs.h>
|
---|
28 | #include <VBox/com/assert.h>
|
---|
29 |
|
---|
30 | namespace com
|
---|
31 | {
|
---|
32 |
|
---|
33 | class EventQueue;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Base class for all events. Intended to be subclassed to introduce new events
|
---|
37 | * and handlers for them.
|
---|
38 | *
|
---|
39 | * Subclasses usually reimplement virtual #handler() (that does nothing by
|
---|
40 | * default) and add new data members describing the event.
|
---|
41 | */
|
---|
42 | class Event
|
---|
43 | {
|
---|
44 | public:
|
---|
45 |
|
---|
46 | Event() {}
|
---|
47 |
|
---|
48 | protected:
|
---|
49 |
|
---|
50 | virtual ~Event() {};
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Event handler. Called in the context of the event queue's thread.
|
---|
54 | * Always reimplemented by subclasses
|
---|
55 | *
|
---|
56 | * @return reserved, should be NULL.
|
---|
57 | */
|
---|
58 | virtual void *handler() { return NULL; }
|
---|
59 |
|
---|
60 | friend class EventQueue;
|
---|
61 | };
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Simple event queue.
|
---|
65 | *
|
---|
66 | * On Linux, if this queue is created on the main thread, it automatically
|
---|
67 | * processes XPCOM/IPC events while waiting for its own (Event) events.
|
---|
68 | */
|
---|
69 | class EventQueue
|
---|
70 | {
|
---|
71 | public:
|
---|
72 |
|
---|
73 | EventQueue();
|
---|
74 | ~EventQueue();
|
---|
75 |
|
---|
76 | BOOL postEvent (Event *event);
|
---|
77 | BOOL waitForEvent (Event **event);
|
---|
78 | BOOL handleEvent (Event *event);
|
---|
79 |
|
---|
80 | private:
|
---|
81 |
|
---|
82 | #if defined (RT_OS_WINDOWS)
|
---|
83 |
|
---|
84 | DWORD mThreadId;
|
---|
85 |
|
---|
86 | #else
|
---|
87 |
|
---|
88 | BOOL mEQCreated;
|
---|
89 |
|
---|
90 | nsCOMPtr <nsIEventQueue> mEventQ;
|
---|
91 | nsCOMPtr <nsIEventQueueService> mEventQService;
|
---|
92 |
|
---|
93 | Event *mLastEvent;
|
---|
94 | BOOL mGotEvent;
|
---|
95 |
|
---|
96 | struct MyPLEvent : public PLEvent
|
---|
97 | {
|
---|
98 | MyPLEvent (Event *e) : event (e) {}
|
---|
99 | Event *event;
|
---|
100 | };
|
---|
101 |
|
---|
102 | static void * PR_CALLBACK plEventHandler (PLEvent* self)
|
---|
103 | {
|
---|
104 | // nsIEventQueue doesn't expose PL_GetEventOwner(), so use an internal
|
---|
105 | // field of PLEvent directly (hackish, but doesn' require an extra lib)
|
---|
106 | EventQueue *owner = (EventQueue *) self->owner;
|
---|
107 | Assert (owner);
|
---|
108 | owner->mLastEvent = ((MyPLEvent *) self)->event;
|
---|
109 | owner->mGotEvent = TRUE;
|
---|
110 | return 0;
|
---|
111 | }
|
---|
112 |
|
---|
113 | static void PR_CALLBACK plEventDestructor (PLEvent* self) { delete self; }
|
---|
114 |
|
---|
115 | #endif
|
---|
116 | };
|
---|
117 |
|
---|
118 | }; // namespace com
|
---|
119 |
|
---|
120 | #endif
|
---|
121 |
|
---|