1 /*
2 SPDX-FileCopyrightText: 2009 Nokia Corporation and /or its subsidiary(-ies).
3 Contact: Qt Software Information (qt-info@nokia.com)
4
5 This file is part of the QtCore module of the Qt Toolkit.
6
7 $QT_BEGIN_LICENSE:LGPL$
8 Commercial Usage
9 Licensees holding valid Qt Commercial licenses may use this file in
10 accordance with the Qt Commercial License Agreement provided with the
11 Software or, alternatively, in accordance with the terms contained in
12 a written agreement between you and Nokia.
13
14 GNU Lesser General Public License Usage
15 Alternatively, this file may be used under the terms of the GNU Lesser
16 General Public License version 2.1 as published by the Free Software
17 Foundation and appearing in the file LICENSE.LGPL included in the
18 packaging of this file. Please review the following information to
19 ensure the GNU Lesser General Public License version 2.1 requirements
20 will be met: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
21
22 In addition, as a special exception, Nokia gives you certain
23 additional rights. These rights are described in the Nokia Qt LGPL
24 Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
25 package.
26
27 GNU General Public License Usage
28 Alternatively, this file may be used under the terms of the GNU
29 General Public License version 3.0 as published by the Free Software
30 Foundation and appearing in the file LICENSE.GPL included in the
31 packaging of this file. Please review the following information to
32 ensure the GNU General Public License version 3.0 requirements will be
33 met: https://www.gnu.org/licenses/gpl-3.0.html.
34
35 If you are unsure which license is appropriate for your use, please
36 contact the sales department at qt-sales@nokia.com.
37 $QT_END_LICENSE$
38
39 */
40
41 #include "qwineventnotifier_p.h"
42
43 //#include "qeventdispatcher_win_p.h"
44 #include "qcoreapplication.h"
45
46 #include <private/qthread_p.h>
47
48 QT_BEGIN_NAMESPACE
49
50 /*
51 \class QWinEventNotifier qwineventnotifier.h
52 \brief The QWinEventNotifier class provides support for the Windows Wait functions.
53
54 \ingroup io
55
56 The QWinEventNotifier class makes it possible to use the wait
57 functions on windows in a asynchronous manner. With this class
58 you can register a HANDLE to an event and get notification when
59 that event becomes signalled. The state of the event is not modified
60 in the process so if it is a manual reset event you will need to
61 reset it after the notification.
62 */
63
64
QWinEventNotifier(QObject * parent)65 QWinEventNotifier::QWinEventNotifier(QObject *parent)
66 : QObject(parent), handleToEvent(0), enabled(false)
67 {}
68
QWinEventNotifier(HANDLE hEvent,QObject * parent)69 QWinEventNotifier::QWinEventNotifier(HANDLE hEvent, QObject *parent)
70 : QObject(parent), handleToEvent(hEvent), enabled(false)
71 {
72 Q_D(QObject);
73 #if 0
74 QEventDispatcherWin32 *eventDispatcher = qobject_cast<QEventDispatcherWin32 *>(d->threadData->eventDispatcher);
75 Q_ASSERT_X(eventDispatcher, "QWinEventNotifier::QWinEventNotifier()",
76 "Cannot create a win event notifier without a QEventDispatcherWin32");
77 eventDispatcher->registerEventNotifier(this);
78 enabled = true;
79 #endif
80 }
81
~QWinEventNotifier()82 QWinEventNotifier::~QWinEventNotifier()
83 {
84 setEnabled(false);
85 }
86
setHandle(HANDLE hEvent)87 void QWinEventNotifier::setHandle(HANDLE hEvent)
88 {
89 setEnabled(false);
90 handleToEvent = hEvent;
91 }
92
handle() const93 HANDLE QWinEventNotifier::handle() const
94 {
95 return handleToEvent;
96 }
97
isEnabled() const98 bool QWinEventNotifier::isEnabled() const
99 {
100 return enabled;
101 }
102
setEnabled(bool enable)103 void QWinEventNotifier::setEnabled(bool enable)
104 {
105 if (enabled == enable) // no change
106 return;
107 enabled = enable;
108
109 Q_D(QObject);
110 #if 0
111 QEventDispatcherWin32 *eventDispatcher = qobject_cast<QEventDispatcherWin32 *>(d->threadData->eventDispatcher);
112 if (!eventDispatcher) // perhaps application is shutting down
113 return;
114
115 if (enabled)
116 eventDispatcher->registerEventNotifier(this);
117 else
118 eventDispatcher->unregisterEventNotifier(this);
119 #endif
120 }
121
event(QEvent * e)122 bool QWinEventNotifier::event(QEvent * e)
123 {
124 if (e->type() == QEvent::ThreadChange) {
125 if (enabled) {
126 QMetaObject::invokeMethod(this, "setEnabled", Qt::QueuedConnection,
127 Q_ARG(bool, enabled));
128 setEnabled(false);
129 }
130 }
131 QObject::event(e); // will activate filters
132 if (e->type() == QEvent::WinEventAct) {
133 emit activated(handleToEvent);
134 return true;
135 }
136 return false;
137 }
138
139 QT_END_NAMESPACE
140