1 | /** @file
|
---|
2 | * MS COM / XPCOM Abstraction Layer - Event and EventQueue class declaration.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2022 Oracle Corporation
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * The contents of this file may alternatively be used under the terms
|
---|
17 | * of the Common Development and Distribution License Version 1.0
|
---|
18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
20 | * CDDL are applicable instead of those of the GPL.
|
---|
21 | *
|
---|
22 | * You may elect to license modified versions of this file under the
|
---|
23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
24 | */
|
---|
25 |
|
---|
26 | #ifndef VBOX_INCLUDED_com_NativeEventQueue_h
|
---|
27 | #define VBOX_INCLUDED_com_NativeEventQueue_h
|
---|
28 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
29 | # pragma once
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | #ifndef VBOX_WITH_XPCOM
|
---|
33 | # include <iprt/win/windows.h>
|
---|
34 | #else
|
---|
35 | # include <nsEventQueueUtils.h>
|
---|
36 | #endif
|
---|
37 |
|
---|
38 | #include <VBox/com/defs.h>
|
---|
39 | #include <VBox/com/assert.h>
|
---|
40 |
|
---|
41 |
|
---|
42 | /** @defgroup grp_com_evt Event and EventQueue Classes
|
---|
43 | * @ingroup grp_com
|
---|
44 | * @{
|
---|
45 | */
|
---|
46 |
|
---|
47 | namespace com
|
---|
48 | {
|
---|
49 |
|
---|
50 | class MainEventQueue;
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Base class for all events. Intended to be subclassed to introduce new
|
---|
54 | * events and handlers for them.
|
---|
55 | *
|
---|
56 | * Subclasses usually reimplement virtual #handler() (that does nothing by
|
---|
57 | * default) and add new data members describing the event.
|
---|
58 | */
|
---|
59 | class NativeEvent
|
---|
60 | {
|
---|
61 | public:
|
---|
62 |
|
---|
63 | NativeEvent() {}
|
---|
64 | virtual ~NativeEvent() {};
|
---|
65 |
|
---|
66 | protected:
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Event handler. Called in the context of the event queue's thread.
|
---|
70 | * Always reimplemented by subclasses
|
---|
71 | *
|
---|
72 | * @return reserved, should be NULL.
|
---|
73 | */
|
---|
74 | virtual void *handler() { return NULL; }
|
---|
75 |
|
---|
76 | friend class NativeEventQueue;
|
---|
77 | };
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Simple event queue.
|
---|
81 | *
|
---|
82 | * When using XPCOM, this will map onto the default XPCOM queue for the thread.
|
---|
83 | * So, if a queue is created on the main thread, it automatically processes
|
---|
84 | * XPCOM/IPC events while waiting.
|
---|
85 | *
|
---|
86 | * When using Windows, Darwin and OS/2, this will map onto the native thread
|
---|
87 | * queue/runloop. So, windows messages and what not will be processed while
|
---|
88 | * waiting for events.
|
---|
89 | *
|
---|
90 | * @note It is intentional that there is no way to retrieve arbitrary
|
---|
91 | * events and controlling their processing. There is no use case which
|
---|
92 | * warrants introducing the complexity of platform independent events.
|
---|
93 | */
|
---|
94 | class NativeEventQueue
|
---|
95 | {
|
---|
96 | public:
|
---|
97 |
|
---|
98 | NativeEventQueue();
|
---|
99 | virtual ~NativeEventQueue();
|
---|
100 |
|
---|
101 | BOOL postEvent(NativeEvent *event);
|
---|
102 | int processEventQueue(RTMSINTERVAL cMsTimeout);
|
---|
103 | int interruptEventQueueProcessing();
|
---|
104 | int getSelectFD();
|
---|
105 | static int init();
|
---|
106 | static int uninit();
|
---|
107 | static NativeEventQueue *getMainEventQueue();
|
---|
108 |
|
---|
109 | #ifdef VBOX_WITH_XPCOM
|
---|
110 | already_AddRefed<nsIEventQueue> getIEventQueue()
|
---|
111 | {
|
---|
112 | return mEventQ.get();
|
---|
113 | }
|
---|
114 | #else
|
---|
115 | static int dispatchMessageOnWindows(MSG const *pMsg, int rc);
|
---|
116 | #endif
|
---|
117 |
|
---|
118 | private:
|
---|
119 | static NativeEventQueue *sMainQueue;
|
---|
120 |
|
---|
121 | #ifndef VBOX_WITH_XPCOM
|
---|
122 |
|
---|
123 | /** The thread which the queue belongs to. */
|
---|
124 | DWORD mThreadId;
|
---|
125 | /** Duplicated thread handle for MsgWaitForMultipleObjects. */
|
---|
126 | HANDLE mhThread;
|
---|
127 |
|
---|
128 | #else // VBOX_WITH_XPCOM
|
---|
129 |
|
---|
130 | /** Whether it was created (and thus needs destroying) or if a queue already
|
---|
131 | * associated with the thread was used. */
|
---|
132 | bool mEQCreated;
|
---|
133 |
|
---|
134 | /** Whether event processing should be interrupted. */
|
---|
135 | bool mInterrupted;
|
---|
136 |
|
---|
137 | nsCOMPtr <nsIEventQueue> mEventQ;
|
---|
138 | nsCOMPtr <nsIEventQueueService> mEventQService;
|
---|
139 |
|
---|
140 | static void *PR_CALLBACK plEventHandler(PLEvent *self);
|
---|
141 | static void PR_CALLBACK plEventDestructor(PLEvent *self);
|
---|
142 |
|
---|
143 | #endif // VBOX_WITH_XPCOM
|
---|
144 | };
|
---|
145 |
|
---|
146 | } /* namespace com */
|
---|
147 |
|
---|
148 | /** @} */
|
---|
149 |
|
---|
150 | #endif /* !VBOX_INCLUDED_com_NativeEventQueue_h */
|
---|
151 |
|
---|