1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtWaylandCompositor module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 or (at your option) any later version |
20 | ** approved by the KDE Free Qt Foundation. The licenses are as published by |
21 | ** the Free Software Foundation and appearing in the file LICENSE.GPL3 |
22 | ** included in the packaging of this file. Please review the following |
23 | ** information to ensure the GNU General Public License requirements will |
24 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
25 | ** |
26 | ** $QT_END_LICENSE$ |
27 | ** |
28 | ****************************************************************************/ |
29 | |
30 | #include "qwaylandinputmethodcontrol.h" |
31 | #include "qwaylandinputmethodcontrol_p.h" |
32 | |
33 | #include "qwaylandcompositor.h" |
34 | #include "qwaylandseat.h" |
35 | #include "qwaylandsurface.h" |
36 | #include "qwaylandview.h" |
37 | #include "qwaylandtextinput.h" |
38 | |
39 | #include <QtGui/QInputMethodEvent> |
40 | |
41 | QWaylandInputMethodControl::QWaylandInputMethodControl(QWaylandSurface *surface) |
42 | : QObject(*new QWaylandInputMethodControlPrivate(surface), surface) |
43 | { |
44 | connect(sender: d_func()->compositor, signal: &QWaylandCompositor::defaultSeatChanged, |
45 | receiver: this, slot: &QWaylandInputMethodControl::defaultSeatChanged); |
46 | QWaylandTextInput *textInput = d_func()->textInput(); |
47 | if (textInput) { |
48 | connect(sender: textInput, signal: &QWaylandTextInput::surfaceEnabled, receiver: this, slot: &QWaylandInputMethodControl::surfaceEnabled); |
49 | connect(sender: textInput, signal: &QWaylandTextInput::surfaceDisabled, receiver: this, slot: &QWaylandInputMethodControl::surfaceDisabled); |
50 | connect(sender: textInput, signal: &QWaylandTextInput::updateInputMethod, receiver: this, slot: &QWaylandInputMethodControl::updateInputMethod); |
51 | } |
52 | } |
53 | |
54 | QVariant QWaylandInputMethodControl::inputMethodQuery(Qt::InputMethodQuery query, QVariant argument) const |
55 | { |
56 | Q_D(const QWaylandInputMethodControl); |
57 | |
58 | QWaylandTextInput *textInput = d->textInput(); |
59 | |
60 | if (textInput && textInput->focus() == d->surface) { |
61 | return textInput->inputMethodQuery(property: query, argument); |
62 | } |
63 | |
64 | return QVariant(); |
65 | } |
66 | |
67 | void QWaylandInputMethodControl::inputMethodEvent(QInputMethodEvent *event) |
68 | { |
69 | Q_D(QWaylandInputMethodControl); |
70 | |
71 | QWaylandTextInput *textInput = d->textInput(); |
72 | if (textInput) { |
73 | textInput->sendInputMethodEvent(event); |
74 | } else { |
75 | event->ignore(); |
76 | } |
77 | } |
78 | |
79 | bool QWaylandInputMethodControl::enabled() const |
80 | { |
81 | Q_D(const QWaylandInputMethodControl); |
82 | |
83 | return d->enabled; |
84 | } |
85 | |
86 | void QWaylandInputMethodControl::setEnabled(bool enabled) |
87 | { |
88 | Q_D(QWaylandInputMethodControl); |
89 | |
90 | if (d->enabled == enabled) |
91 | return; |
92 | |
93 | d->enabled = enabled; |
94 | emit enabledChanged(enabled); |
95 | emit updateInputMethod(queries: Qt::ImQueryInput); |
96 | } |
97 | |
98 | void QWaylandInputMethodControl::surfaceEnabled(QWaylandSurface *surface) |
99 | { |
100 | Q_D(QWaylandInputMethodControl); |
101 | |
102 | if (surface == d->surface) |
103 | setEnabled(true); |
104 | } |
105 | |
106 | void QWaylandInputMethodControl::surfaceDisabled(QWaylandSurface *surface) |
107 | { |
108 | Q_D(QWaylandInputMethodControl); |
109 | |
110 | if (surface == d->surface) |
111 | setEnabled(false); |
112 | } |
113 | |
114 | void QWaylandInputMethodControl::setSurface(QWaylandSurface *surface) |
115 | { |
116 | Q_D(QWaylandInputMethodControl); |
117 | |
118 | if (d->surface == surface) |
119 | return; |
120 | |
121 | d->surface = surface; |
122 | |
123 | QWaylandTextInput *textInput = d->textInput(); |
124 | setEnabled(textInput && textInput->isSurfaceEnabled(surface: d->surface)); |
125 | } |
126 | |
127 | void QWaylandInputMethodControl::defaultSeatChanged() |
128 | { |
129 | Q_D(QWaylandInputMethodControl); |
130 | |
131 | disconnect(sender: d->textInput(), signal: nullptr, receiver: this, member: nullptr); |
132 | |
133 | d->seat = d->compositor->defaultSeat(); |
134 | QWaylandTextInput *textInput = d->textInput(); |
135 | |
136 | connect(sender: textInput, signal: &QWaylandTextInput::surfaceEnabled, receiver: this, slot: &QWaylandInputMethodControl::surfaceEnabled); |
137 | connect(sender: textInput, signal: &QWaylandTextInput::surfaceDisabled, receiver: this, slot: &QWaylandInputMethodControl::surfaceDisabled); |
138 | |
139 | setEnabled(textInput && textInput->isSurfaceEnabled(surface: d->surface)); |
140 | } |
141 | |
142 | QWaylandInputMethodControlPrivate::QWaylandInputMethodControlPrivate(QWaylandSurface *surface) |
143 | : compositor(surface->compositor()) |
144 | , seat(compositor->defaultSeat()) |
145 | , surface(surface) |
146 | { |
147 | } |
148 | |
149 | QWaylandTextInput *QWaylandInputMethodControlPrivate::textInput() const |
150 | { |
151 | return QWaylandTextInput::findIn(container: seat); |
152 | } |
153 | |