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 (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 |
|
---|
27 | #ifndef ___VBox_com_EventQueue_h
|
---|
28 | #define ___VBox_com_EventQueue_h
|
---|
29 |
|
---|
30 | #if !defined (VBOX_WITH_XPCOM)
|
---|
31 | #include <windows.h>
|
---|
32 | #else
|
---|
33 | #include <nsEventQueueUtils.h>
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | #include <VBox/com/defs.h>
|
---|
37 | #include <VBox/com/assert.h>
|
---|
38 |
|
---|
39 | namespace com
|
---|
40 | {
|
---|
41 |
|
---|
42 | class EventQueue;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Base class for all events. Intended to be subclassed to introduce new events
|
---|
46 | * and handlers for them.
|
---|
47 | *
|
---|
48 | * Subclasses usually reimplement virtual #handler() (that does nothing by
|
---|
49 | * default) and add new data members describing the event.
|
---|
50 | */
|
---|
51 | class Event
|
---|
52 | {
|
---|
53 | public:
|
---|
54 |
|
---|
55 | Event() {}
|
---|
56 |
|
---|
57 | protected:
|
---|
58 |
|
---|
59 | virtual ~Event() {};
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Event handler. Called in the context of the event queue's thread.
|
---|
63 | * Always reimplemented by subclasses
|
---|
64 | *
|
---|
65 | * @return reserved, should be NULL.
|
---|
66 | */
|
---|
67 | virtual void *handler() { return NULL; }
|
---|
68 |
|
---|
69 | friend class EventQueue;
|
---|
70 | };
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Simple event queue.
|
---|
74 | *
|
---|
75 | * On Linux, if this queue is created on the main thread, it automatically
|
---|
76 | * processes XPCOM/IPC events while waiting for its own (Event) events.
|
---|
77 | */
|
---|
78 | class EventQueue
|
---|
79 | {
|
---|
80 | public:
|
---|
81 |
|
---|
82 | EventQueue();
|
---|
83 | ~EventQueue();
|
---|
84 |
|
---|
85 | BOOL postEvent (Event *event);
|
---|
86 | BOOL waitForEvent (Event **event);
|
---|
87 | BOOL handleEvent (Event *event);
|
---|
88 |
|
---|
89 | private:
|
---|
90 |
|
---|
91 | #if !defined (VBOX_WITH_XPCOM)
|
---|
92 |
|
---|
93 | DWORD mThreadId;
|
---|
94 |
|
---|
95 | #else
|
---|
96 |
|
---|
97 | BOOL mEQCreated;
|
---|
98 |
|
---|
99 | nsCOMPtr <nsIEventQueue> mEventQ;
|
---|
100 | nsCOMPtr <nsIEventQueueService> mEventQService;
|
---|
101 |
|
---|
102 | Event *mLastEvent;
|
---|
103 | BOOL mGotEvent;
|
---|
104 |
|
---|
105 | struct MyPLEvent : public PLEvent
|
---|
106 | {
|
---|
107 | MyPLEvent (Event *e) : event (e) {}
|
---|
108 | Event *event;
|
---|
109 | };
|
---|
110 |
|
---|
111 | static void * PR_CALLBACK plEventHandler (PLEvent* self)
|
---|
112 | {
|
---|
113 | // nsIEventQueue doesn't expose PL_GetEventOwner(), so use an internal
|
---|
114 | // field of PLEvent directly (hackish, but doesn' require an extra lib)
|
---|
115 | EventQueue *owner = (EventQueue *) self->owner;
|
---|
116 | Assert (owner);
|
---|
117 | owner->mLastEvent = ((MyPLEvent *) self)->event;
|
---|
118 | owner->mGotEvent = TRUE;
|
---|
119 | return 0;
|
---|
120 | }
|
---|
121 |
|
---|
122 | static void PR_CALLBACK plEventDestructor (PLEvent* self) { delete self; }
|
---|
123 |
|
---|
124 | #endif
|
---|
125 | };
|
---|
126 |
|
---|
127 | } /* namespace com */
|
---|
128 |
|
---|
129 | #endif
|
---|
130 |
|
---|