-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathxcbinputwindow.cpp
More file actions
313 lines (275 loc) · 10.1 KB
/
Copy pathxcbinputwindow.cpp
File metadata and controls
313 lines (275 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
* SPDX-FileCopyrightText: 2017-2017 CSSlayer <wengxt@gmail.com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
*/
#include "xcbinputwindow.h"
#include <unistd.h>
#include <algorithm>
#include <climits>
#include <cstdint>
#include <vector>
#include <cairo.h>
#include <xcb/xcb.h>
#include <xcb/xcb_aux.h>
#include <xcb/xcb_ewmh.h>
#include <xcb/xcb_icccm.h>
#include <xcb/xproto.h>
#include "fcitx-utils/rect.h"
#include "fcitx/inputcontext.h"
#include "inputwindow.h"
#include "theme.h"
#include "xcb_public.h"
#include "xcbui.h"
#include "xcbwindow.h"
namespace fcitx::classicui {
XCBInputWindow::XCBInputWindow(XCBUI *ui)
: XCBWindow(ui), InputWindow(ui->parent()),
atomBlur_(ui_->parent()->xcb()->call<IXCBModule::atom>(
ui_->displayName(), "_KDE_NET_WM_BLUR_BEHIND_REGION", false)) {}
void XCBInputWindow::postCreateWindow() {
if (ui_->ewmh()->_NET_WM_WINDOW_TYPE_COMBO &&
ui_->ewmh()->_NET_WM_WINDOW_TYPE) {
xcb_ewmh_set_wm_window_type(ui_->ewmh(), wid_, 1,
&ui_->ewmh()->_NET_WM_WINDOW_TYPE_COMBO);
}
if (ui_->ewmh()->_NET_WM_PID) {
xcb_ewmh_set_wm_pid(ui_->ewmh(), wid_, getpid());
}
const char name[] = "Fcitx5 Input Window";
xcb_icccm_set_wm_name(ui_->connection(), wid_, XCB_ATOM_STRING, 8,
sizeof(name) - 1, name);
const char klass[] = "fcitx\0fcitx";
xcb_icccm_set_wm_class(ui_->connection(), wid_, sizeof(klass) - 1, klass);
addEventMaskToWindow(
ui_->connection(), wid_,
XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE |
XCB_EVENT_MASK_POINTER_MOTION | XCB_EVENT_MASK_EXPOSURE |
XCB_EVENT_MASK_LEAVE_WINDOW);
}
const Rect *XCBInputWindow::getClosestScreen(const Rect &cursorRect) const {
const Rect *closestScreen = nullptr;
int shortestDistance = INT_MAX;
for (const auto &rect : ui_->screenRects()) {
int thisDistance =
rect.first.distance(cursorRect.left(), cursorRect.top());
if (thisDistance < shortestDistance) {
shortestDistance = thisDistance;
closestScreen = &rect.first;
}
}
return closestScreen;
}
int XCBInputWindow::calculatePositionX(const Rect &cursorRect,
const Rect *closestScreen) const {
// TODO: RTL support.
int x = cursorRect.left();
auto &theme = parent_->theme();
int leftSW =
physicalFromLogical(theme.inputPanel->shadowMargin->marginLeft.value());
int rightSW = physicalFromLogical(
theme.inputPanel->shadowMargin->marginRight.value());
int actualWidth = physicalWidth_ - leftSW - rightSW;
actualWidth = actualWidth <= 0 ? physicalWidth_ : actualWidth;
if (closestScreen != nullptr) {
int newX = std::max(x, closestScreen->left());
if (newX + actualWidth > closestScreen->right()) {
newX = closestScreen->right() - actualWidth;
}
x = std::max(newX, closestScreen->left());
}
// exclude shadow border width
x -= leftSW;
return x;
}
int XCBInputWindow::calculatePositionY(const Rect &cursorRect,
const Rect *closestScreen) const {
// TODO: RTL support.
int y = cursorRect.top();
auto &theme = parent_->theme();
int topSW =
physicalFromLogical(theme.inputPanel->shadowMargin->marginTop.value());
int bottomSW = physicalFromLogical(
theme.inputPanel->shadowMargin->marginBottom.value());
int actualHeight = physicalHeight_ - topSW - bottomSW;
actualHeight = actualHeight <= 0 ? physicalHeight_ : actualHeight;
if (closestScreen != nullptr) {
int h = cursorRect.height();
int newY;
if (y < closestScreen->top()) {
newY = closestScreen->top();
} else {
newY = y + (h ? h : physicalFromLogical(10));
}
// Try flip y.
if (newY + actualHeight > closestScreen->bottom()) {
if (newY > closestScreen->bottom()) {
newY = closestScreen->bottom() - actualHeight -
physicalFromLogical(40);
} else { /* better position the window */
newY = newY - actualHeight -
((h == 0) ? physicalFromLogical(40) : h);
}
// If after flip, top is out of the screen, we still prefer the top
// edge to be always with in screen.
if (newY < closestScreen->top()) {
newY = closestScreen->top();
}
}
y = newY;
}
// exclude shadow border width
y -= topSW;
return y;
}
void XCBInputWindow::updatePosition(InputContext *inputContext) {
if (!visible()) {
return;
}
const Rect &cursorRect = inputContext->cursorRect();
const Rect *closestScreen = getClosestScreen(cursorRect);
xcb_params_configure_window_t wc;
wc.x = calculatePositionX(cursorRect, closestScreen);
wc.y = calculatePositionY(cursorRect, closestScreen);
wc.stack_mode = XCB_STACK_MODE_ABOVE;
xcb_aux_configure_window(ui_->connection(), wid_,
XCB_CONFIG_WINDOW_STACK_MODE |
XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y,
&wc);
}
void XCBInputWindow::updateDPI(InputContext *inputContext) {
dpi_ = ui_->dpiByPosition(inputContext->cursorRect().left(),
inputContext->cursorRect().top());
// Let Cairo device scale handle HiDPI so Pango keeps logical font sizes.
setFontDPI(-1);
}
double XCBInputWindow::scale() const { return scaleForDPI(dpi_); }
void XCBInputWindow::update(InputContext *inputContext) {
if (!wid_) {
return;
}
auto oldVisible = visible();
if (inputContext) {
updateDPI(inputContext);
}
auto [width, height] = InputWindow::update(inputContext);
if (!visible()) {
if (oldVisible) {
xcb_unmap_window(ui_->connection(), wid_);
hoverIndex_ = -1;
}
return;
}
auto oldPhysicalWidth = physicalWidth_;
auto oldPhysicalHeight = physicalHeight_;
resize(width, height);
if (physicalWidth_ != oldPhysicalWidth ||
physicalHeight_ != oldPhysicalHeight) {
if (atomBlur_) {
Rect logicalRect(0, 0, width, height);
shrink(logicalRect, *ui_->parent()->theme().inputPanel->blurMargin);
if (!*ui_->parent()->theme().inputPanel->enableBlur ||
logicalRect.isEmpty()) {
xcb_delete_property(ui_->connection(), wid_, atomBlur_);
} else {
auto rect = physicalFromLogical(logicalRect);
std::vector<uint32_t> data;
if (ui_->parent()->theme().inputPanel->blurMask->empty()) {
data.push_back(rect.left());
data.push_back(rect.top());
data.push_back(rect.width());
data.push_back(rect.height());
xcb_change_property(ui_->connection(),
XCB_PROP_MODE_REPLACE, wid_, atomBlur_,
XCB_ATOM_CARDINAL, 32, data.size(),
data.data());
} else {
auto region = parent_->theme().mask(
parent_->theme().maskConfig(), width, height);
for (const auto &rect : region) {
auto physicalRect = physicalFromLogical(rect);
data.push_back(physicalRect.left());
data.push_back(physicalRect.top());
data.push_back(physicalRect.width());
data.push_back(physicalRect.height());
}
xcb_change_property(ui_->connection(),
XCB_PROP_MODE_REPLACE, wid_, atomBlur_,
XCB_ATOM_CARDINAL, 32, data.size(),
data.data());
}
}
}
}
cairo_t *c = cairo_create(prerender());
updatePosition(inputContext);
if (!oldVisible) {
xcb_map_window(ui_->connection(), wid_);
}
paint(c, width, height);
cairo_destroy(c);
render();
}
bool XCBInputWindow::filterEvent(xcb_generic_event_t *event) {
uint8_t response_type = event->response_type & ~0x80;
switch (response_type) {
case XCB_EXPOSE: {
auto *expose = reinterpret_cast<xcb_expose_event_t *>(event);
if (expose->window == wid_) {
repaint();
return true;
}
break;
}
case XCB_BUTTON_PRESS: {
auto *buttonPress = reinterpret_cast<xcb_button_press_event_t *>(event);
if (buttonPress->event != wid_) {
break;
}
if (buttonPress->detail == XCB_BUTTON_INDEX_1) {
click(logicalFromPhysical(buttonPress->event_x),
logicalFromPhysical(buttonPress->event_y));
} else if (buttonPress->detail == XCB_BUTTON_INDEX_4) {
wheel(/*up=*/true);
} else if (buttonPress->detail == XCB_BUTTON_INDEX_5) {
wheel(/*up=*/false);
}
return true;
}
case XCB_MOTION_NOTIFY: {
auto *motion = reinterpret_cast<xcb_motion_notify_event_t *>(event);
if (motion->event == wid_) {
if (hover(logicalFromPhysical(motion->event_x),
logicalFromPhysical(motion->event_y))) {
repaint();
}
return true;
}
break;
}
case XCB_LEAVE_NOTIFY: {
auto *leave = reinterpret_cast<xcb_leave_notify_event_t *>(event);
if (leave->event == wid_) {
if (hover(-1, -1)) {
repaint();
}
return true;
}
break;
}
}
return false;
}
void XCBInputWindow::repaint() {
if (!visible()) {
return;
}
if (auto *surface = prerender()) {
cairo_t *c = cairo_create(surface);
paint(c, width(), height());
cairo_destroy(c);
render();
}
}
} // namespace fcitx::classicui