-
Notifications
You must be signed in to change notification settings - Fork 18
/
events.cpp
1386 lines (1287 loc) · 55.2 KB
/
events.cpp
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 1999, 2000 Matthias Ettrich <[email protected]>
Copyright (C) 2003 Lubos Lunak <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
/*
This file contains things relevant to handling incoming events.
*/
#include "x11client.h"
#include "cursor.h"
#include "focuschain.h"
#include "netinfo.h"
#include "workspace.h"
#include "atoms.h"
#ifdef KWIN_BUILD_TABBOX
#include "tabbox.h"
#endif
#include "group.h"
#include "rules.h"
#include "unmanaged.h"
#include "useractions.h"
#include "effects.h"
#include "screens.h"
#include "xcbutils.h"
#include <KDecoration2/Decoration>
#include <QApplication>
#include <QDebug>
#include <QHoverEvent>
#include <QKeyEvent>
#include <QMouseEvent>
#include <QStyleHints>
#include <QWheelEvent>
#include <kkeyserver.h>
#include <xcb/sync.h>
#ifdef XCB_ICCCM_FOUND
#include <xcb/xcb_icccm.h>
#endif
#include "composite.h"
#include "x11eventfilter.h"
#include "pointer_input.h"
#include "wayland_server.h"
#include <KWayland/Server/surface_interface.h>
#ifndef XCB_GE_GENERIC
#define XCB_GE_GENERIC 35
typedef struct xcb_ge_generic_event_t {
uint8_t response_type; /**< */
uint8_t extension; /**< */
uint16_t sequence; /**< */
uint32_t length; /**< */
uint16_t event_type; /**< */
uint8_t pad0[22]; /**< */
uint32_t full_sequence; /**< */
} xcb_ge_generic_event_t;
#endif
namespace KWin
{
// ****************************************
// Workspace
// ****************************************
static xcb_window_t findEventWindow(xcb_generic_event_t *event)
{
const uint8_t eventType = event->response_type & ~0x80;
switch(eventType) {
case XCB_KEY_PRESS:
case XCB_KEY_RELEASE:
return reinterpret_cast<xcb_key_press_event_t*>(event)->event;
case XCB_BUTTON_PRESS:
case XCB_BUTTON_RELEASE:
return reinterpret_cast<xcb_button_press_event_t*>(event)->event;
case XCB_MOTION_NOTIFY:
return reinterpret_cast<xcb_motion_notify_event_t*>(event)->event;
case XCB_ENTER_NOTIFY:
case XCB_LEAVE_NOTIFY:
return reinterpret_cast<xcb_enter_notify_event_t*>(event)->event;
case XCB_FOCUS_IN:
case XCB_FOCUS_OUT:
return reinterpret_cast<xcb_focus_in_event_t*>(event)->event;
case XCB_EXPOSE:
return reinterpret_cast<xcb_expose_event_t*>(event)->window;
case XCB_GRAPHICS_EXPOSURE:
return reinterpret_cast<xcb_graphics_exposure_event_t*>(event)->drawable;
case XCB_NO_EXPOSURE:
return reinterpret_cast<xcb_no_exposure_event_t*>(event)->drawable;
case XCB_VISIBILITY_NOTIFY:
return reinterpret_cast<xcb_visibility_notify_event_t*>(event)->window;
case XCB_CREATE_NOTIFY:
return reinterpret_cast<xcb_create_notify_event_t*>(event)->window;
case XCB_DESTROY_NOTIFY:
return reinterpret_cast<xcb_destroy_notify_event_t*>(event)->window;
case XCB_UNMAP_NOTIFY:
return reinterpret_cast<xcb_unmap_notify_event_t*>(event)->window;
case XCB_MAP_NOTIFY:
return reinterpret_cast<xcb_map_notify_event_t*>(event)->window;
case XCB_MAP_REQUEST:
return reinterpret_cast<xcb_map_request_event_t*>(event)->window;
case XCB_REPARENT_NOTIFY:
return reinterpret_cast<xcb_reparent_notify_event_t*>(event)->window;
case XCB_CONFIGURE_NOTIFY:
return reinterpret_cast<xcb_configure_notify_event_t*>(event)->window;
case XCB_CONFIGURE_REQUEST:
return reinterpret_cast<xcb_configure_request_event_t*>(event)->window;
case XCB_GRAVITY_NOTIFY:
return reinterpret_cast<xcb_gravity_notify_event_t*>(event)->window;
case XCB_RESIZE_REQUEST:
return reinterpret_cast<xcb_resize_request_event_t*>(event)->window;
case XCB_CIRCULATE_NOTIFY:
case XCB_CIRCULATE_REQUEST:
return reinterpret_cast<xcb_circulate_notify_event_t*>(event)->window;
case XCB_PROPERTY_NOTIFY:
return reinterpret_cast<xcb_property_notify_event_t*>(event)->window;
case XCB_COLORMAP_NOTIFY:
return reinterpret_cast<xcb_colormap_notify_event_t*>(event)->window;
case XCB_CLIENT_MESSAGE:
return reinterpret_cast<xcb_client_message_event_t*>(event)->window;
default:
// extension handling
if (eventType == Xcb::Extensions::self()->shapeNotifyEvent()) {
return reinterpret_cast<xcb_shape_notify_event_t*>(event)->affected_window;
}
if (eventType == Xcb::Extensions::self()->damageNotifyEvent()) {
return reinterpret_cast<xcb_damage_notify_event_t*>(event)->drawable;
}
return XCB_WINDOW_NONE;
}
}
QVector<QByteArray> s_xcbEerrors({
QByteArrayLiteral("Success"),
QByteArrayLiteral("BadRequest"),
QByteArrayLiteral("BadValue"),
QByteArrayLiteral("BadWindow"),
QByteArrayLiteral("BadPixmap"),
QByteArrayLiteral("BadAtom"),
QByteArrayLiteral("BadCursor"),
QByteArrayLiteral("BadFont"),
QByteArrayLiteral("BadMatch"),
QByteArrayLiteral("BadDrawable"),
QByteArrayLiteral("BadAccess"),
QByteArrayLiteral("BadAlloc"),
QByteArrayLiteral("BadColor"),
QByteArrayLiteral("BadGC"),
QByteArrayLiteral("BadIDChoice"),
QByteArrayLiteral("BadName"),
QByteArrayLiteral("BadLength"),
QByteArrayLiteral("BadImplementation"),
QByteArrayLiteral("Unknown")});
void Workspace::registerEventFilter(X11EventFilter *filter)
{
if (filter->isGenericEvent())
m_genericEventFilters.append(filter);
else
m_eventFilters.append(filter);
}
void Workspace::unregisterEventFilter(X11EventFilter *filter)
{
if (filter->isGenericEvent())
m_genericEventFilters.removeOne(filter);
else
m_eventFilters.removeOne(filter);
}
/**
* Handles workspace specific XCB event
*/
bool Workspace::workspaceEvent(xcb_generic_event_t *e)
{
const uint8_t eventType = e->response_type & ~0x80;
if (!eventType) {
// let's check whether it's an error from one of the extensions KWin uses
xcb_generic_error_t *error = reinterpret_cast<xcb_generic_error_t*>(e);
const QVector<Xcb::ExtensionData> extensions = Xcb::Extensions::self()->extensions();
for (const auto &extension : extensions) {
if (error->major_code == extension.majorOpcode) {
QByteArray errorName;
if (error->error_code < s_xcbEerrors.size()) {
errorName = s_xcbEerrors.at(error->error_code);
} else if (error->error_code >= extension.errorBase) {
const int index = error->error_code - extension.errorBase;
if (index >= 0 && index < extension.errorCodes.size()) {
errorName = extension.errorCodes.at(index);
}
}
if (errorName.isEmpty()) {
errorName = QByteArrayLiteral("Unknown");
}
qCWarning(KWIN_CORE, "XCB error: %d (%s), sequence: %d, resource id: %d, major code: %d (%s), minor code: %d (%s)",
int(error->error_code), errorName.constData(),
int(error->sequence), int(error->resource_id),
int(error->major_code), extension.name.constData(),
int(error->minor_code),
extension.opCodes.size() > error->minor_code ? extension.opCodes.at(error->minor_code).constData() : "Unknown");
return true;
}
}
return false;
}
if (eventType == XCB_GE_GENERIC) {
xcb_ge_generic_event_t *ge = reinterpret_cast<xcb_ge_generic_event_t *>(e);
foreach (X11EventFilter *filter, m_genericEventFilters) {
if (filter->extension() == ge->extension && filter->genericEventTypes().contains(ge->event_type) && filter->event(e)) {
return true;
}
}
} else {
foreach (X11EventFilter *filter, m_eventFilters) {
if (filter->eventTypes().contains(eventType) && filter->event(e)) {
return true;
}
}
}
if (effects && static_cast< EffectsHandlerImpl* >(effects)->hasKeyboardGrab()
&& (eventType == XCB_KEY_PRESS || eventType == XCB_KEY_RELEASE))
return false; // let Qt process it, it'll be intercepted again in eventFilter()
// events that should be handled before Clients can get them
switch (eventType) {
case XCB_CONFIGURE_NOTIFY:
if (reinterpret_cast<xcb_configure_notify_event_t*>(e)->event == rootWindow())
markXStackingOrderAsDirty();
break;
};
const xcb_window_t eventWindow = findEventWindow(e);
if (eventWindow != XCB_WINDOW_NONE) {
if (X11Client *c = findClient(Predicate::WindowMatch, eventWindow)) {
if (c->windowEvent(e))
return true;
} else if (X11Client *c = findClient(Predicate::WrapperIdMatch, eventWindow)) {
if (c->windowEvent(e))
return true;
} else if (X11Client *c = findClient(Predicate::FrameIdMatch, eventWindow)) {
if (c->windowEvent(e))
return true;
} else if (X11Client *c = findClient(Predicate::InputIdMatch, eventWindow)) {
if (c->windowEvent(e))
return true;
} else if (Unmanaged* c = findUnmanaged(eventWindow)) {
if (c->windowEvent(e))
return true;
}
}
switch (eventType) {
case XCB_CREATE_NOTIFY: {
const auto *event = reinterpret_cast<xcb_create_notify_event_t*>(e);
if (event->parent == rootWindow() &&
!QWidget::find(event->window) &&
!event->override_redirect) {
// see comments for allowClientActivation()
updateXTime();
const xcb_timestamp_t t = xTime();
xcb_change_property(connection(), XCB_PROP_MODE_REPLACE, event->window, atoms->kde_net_wm_user_creation_time, XCB_ATOM_CARDINAL, 32, 1, &t);
}
break;
}
case XCB_UNMAP_NOTIFY: {
const auto *event = reinterpret_cast<xcb_unmap_notify_event_t*>(e);
return (event->event != event->window); // hide wm typical event from Qt
}
case XCB_REPARENT_NOTIFY: {
//do not confuse Qt with these events. After all, _we_ are the
//window manager who does the reparenting.
return true;
}
case XCB_MAP_REQUEST: {
updateXTime();
const auto *event = reinterpret_cast<xcb_map_request_event_t*>(e);
if (X11Client *c = findClient(Predicate::WindowMatch, event->window)) {
// e->xmaprequest.window is different from e->xany.window
// TODO this shouldn't be necessary now
c->windowEvent(e);
FocusChain::self()->update(c, FocusChain::Update);
} else if ( true /*|| e->xmaprequest.parent != root */ ) {
// NOTICE don't check for the parent being the root window, this breaks when some app unmaps
// a window, changes something and immediately maps it back, without giving KWin
// a chance to reparent it back to root
// since KWin can get MapRequest only for root window children and
// children of WindowWrapper (=clients), the check is AFAIK useless anyway
// NOTICE: The save-set support in X11Client::mapRequestEvent() actually requires that
// this code doesn't check the parent to be root.
if (!createClient(event->window, false)) {
xcb_map_window(connection(), event->window);
const uint32_t values[] = { XCB_STACK_MODE_ABOVE };
xcb_configure_window(connection(), event->window, XCB_CONFIG_WINDOW_STACK_MODE, values);
}
}
return true;
}
case XCB_MAP_NOTIFY: {
const auto *event = reinterpret_cast<xcb_map_notify_event_t*>(e);
if (event->override_redirect) {
Unmanaged* c = findUnmanaged(event->window);
if (c == nullptr)
c = createUnmanaged(event->window);
if (c) {
// if hasScheduledRelease is true, it means a unamp and map sequence has occurred.
// since release is scheduled after map notify, this old Unmanaged will get released
// before KWIN has chance to remanage it again. so release it right now.
if (c->hasScheduledRelease()) {
c->release();
c = createUnmanaged(event->window);
}
if (c)
return c->windowEvent(e);
}
}
return (event->event != event->window); // hide wm typical event from Qt
}
case XCB_CONFIGURE_REQUEST: {
const auto *event = reinterpret_cast<xcb_configure_request_event_t*>(e);
if (event->parent == rootWindow()) {
uint32_t values[5] = { 0, 0, 0, 0, 0};
const uint32_t value_mask = event->value_mask
& (XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT | XCB_CONFIG_WINDOW_BORDER_WIDTH);
int i = 0;
if (value_mask & XCB_CONFIG_WINDOW_X) {
values[i++] = event->x;
}
if (value_mask & XCB_CONFIG_WINDOW_Y) {
values[i++] = event->y;
}
if (value_mask & XCB_CONFIG_WINDOW_WIDTH) {
values[i++] = event->width;
}
if (value_mask & XCB_CONFIG_WINDOW_HEIGHT) {
values[i++] = event->height;
}
if (value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
values[i++] = event->border_width;
}
xcb_configure_window(connection(), event->window, value_mask, values);
return true;
}
break;
}
case XCB_FOCUS_IN: {
const auto *event = reinterpret_cast<xcb_focus_in_event_t*>(e);
if (event->event == rootWindow()
&& (event->detail == XCB_NOTIFY_DETAIL_NONE || event->detail == XCB_NOTIFY_DETAIL_POINTER_ROOT || event->detail == XCB_NOTIFY_DETAIL_INFERIOR)) {
Xcb::CurrentInput currentInput;
updateXTime(); // focusToNull() uses xTime(), which is old now (FocusIn has no timestamp)
// it seems we can "loose" focus reversions when the closing client hold a grab
// => catch the typical pattern (though we don't want the focus on the root anyway) #348935
const bool lostFocusPointerToRoot = currentInput->focus == rootWindow() && event->detail == XCB_NOTIFY_DETAIL_INFERIOR;
if (!currentInput.isNull() && (currentInput->focus == XCB_WINDOW_NONE || currentInput->focus == XCB_INPUT_FOCUS_POINTER_ROOT || lostFocusPointerToRoot)) {
//kWarning( 1212 ) << "X focus set to None/PointerRoot, reseting focus" ;
AbstractClient *c = mostRecentlyActivatedClient();
if (c != nullptr)
requestFocus(c, true);
else if (activateNextClient(nullptr))
; // ok, activated
else
focusToNull();
}
}
}
// fall through
case XCB_FOCUS_OUT:
return true; // always eat these, they would tell Qt that KWin is the active app
default:
break;
}
return false;
}
// Used only to filter events that need to be processed by Qt first
// (e.g. keyboard input to be composed), otherwise events are
// handle by the XEvent filter above
bool Workspace::workspaceEvent(QEvent* e)
{
if ((e->type() == QEvent::KeyPress || e->type() == QEvent::KeyRelease || e->type() == QEvent::ShortcutOverride)
&& effects && static_cast< EffectsHandlerImpl* >(effects)->hasKeyboardGrab()) {
static_cast< EffectsHandlerImpl* >(effects)->grabbedKeyboardEvent(static_cast< QKeyEvent* >(e));
return true;
}
return false;
}
// ****************************************
// Client
// ****************************************
/**
* General handler for XEvents concerning the client window
*/
bool X11Client::windowEvent(xcb_generic_event_t *e)
{
if (findEventWindow(e) == window()) { // avoid doing stuff on frame or wrapper
NET::Properties dirtyProperties;
NET::Properties2 dirtyProperties2;
double old_opacity = opacity();
info->event(e, &dirtyProperties, &dirtyProperties2); // pass through the NET stuff
if ((dirtyProperties & NET::WMName) != 0)
fetchName();
if ((dirtyProperties & NET::WMIconName) != 0)
fetchIconicName();
if ((dirtyProperties & NET::WMStrut) != 0
|| (dirtyProperties2 & NET::WM2ExtendedStrut) != 0) {
workspace()->updateClientArea();
}
if ((dirtyProperties & NET::WMIcon) != 0)
getIcons();
// Note there's a difference between userTime() and info->userTime()
// info->userTime() is the value of the property, userTime() also includes
// updates of the time done by KWin (ButtonPress on windowrapper etc.).
if ((dirtyProperties2 & NET::WM2UserTime) != 0) {
workspace()->setWasUserInteraction();
updateUserTime(info->userTime());
}
if ((dirtyProperties2 & NET::WM2StartupId) != 0)
startupIdChanged();
if (dirtyProperties2 & NET::WM2Opacity) {
if (compositing()) {
addRepaintFull();
emit opacityChanged(this, old_opacity);
} else {
// forward to the frame if there's possibly another compositing manager running
NETWinInfo i(connection(), frameId(), rootWindow(), NET::Properties(), NET::Properties2());
i.setOpacity(info->opacity());
}
}
if (dirtyProperties2 & NET::WM2FrameOverlap) {
// ### Inform the decoration
}
if (dirtyProperties2.testFlag(NET::WM2WindowRole)) {
emit windowRoleChanged();
}
if (dirtyProperties2.testFlag(NET::WM2WindowClass)) {
getResourceClass();
}
if (dirtyProperties2.testFlag(NET::WM2BlockCompositing)) {
setBlockingCompositing(info->isBlockingCompositing());
}
if (dirtyProperties2.testFlag(NET::WM2GroupLeader)) {
checkGroup();
updateAllowedActions(); // Group affects isMinimizable()
}
if (dirtyProperties2.testFlag(NET::WM2Urgency)) {
updateUrgency();
}
if (dirtyProperties2 & NET::WM2OpaqueRegion) {
getWmOpaqueRegion();
}
if (dirtyProperties2 & NET::WM2DesktopFileName) {
setDesktopFileName(QByteArray(info->desktopFileName()));
}
if (dirtyProperties2 & NET::WM2GTKFrameExtents) {
setClientFrameExtents(info->gtkFrameExtents());
}
}
const uint8_t eventType = e->response_type & ~0x80;
switch(eventType) {
case XCB_UNMAP_NOTIFY:
unmapNotifyEvent(reinterpret_cast<xcb_unmap_notify_event_t*>(e));
break;
case XCB_DESTROY_NOTIFY:
destroyNotifyEvent(reinterpret_cast<xcb_destroy_notify_event_t*>(e));
break;
case XCB_MAP_REQUEST:
// this one may pass the event to workspace
return mapRequestEvent(reinterpret_cast<xcb_map_request_event_t*>(e));
case XCB_CONFIGURE_REQUEST:
configureRequestEvent(reinterpret_cast<xcb_configure_request_event_t*>(e));
break;
case XCB_PROPERTY_NOTIFY:
propertyNotifyEvent(reinterpret_cast<xcb_property_notify_event_t*>(e));
break;
case XCB_KEY_PRESS:
updateUserTime(reinterpret_cast<xcb_key_press_event_t*>(e)->time);
break;
case XCB_BUTTON_PRESS: {
const auto *event = reinterpret_cast<xcb_button_press_event_t*>(e);
updateUserTime(event->time);
buttonPressEvent(event->event, event->detail, event->state,
event->event_x, event->event_y, event->root_x, event->root_y, event->time);
break;
}
case XCB_KEY_RELEASE:
// don't update user time on releases
// e.g. if the user presses Alt+F2, the Alt release
// would appear as user input to the currently active window
break;
case XCB_BUTTON_RELEASE: {
const auto *event = reinterpret_cast<xcb_button_release_event_t*>(e);
// don't update user time on releases
// e.g. if the user presses Alt+F2, the Alt release
// would appear as user input to the currently active window
buttonReleaseEvent(event->event, event->detail, event->state,
event->event_x, event->event_y, event->root_x, event->root_y);
break;
}
case XCB_MOTION_NOTIFY: {
const auto *event = reinterpret_cast<xcb_motion_notify_event_t*>(e);
motionNotifyEvent(event->event, event->state,
event->event_x, event->event_y, event->root_x, event->root_y);
workspace()->updateFocusMousePosition(QPoint(event->root_x, event->root_y));
break;
}
case XCB_ENTER_NOTIFY: {
auto *event = reinterpret_cast<xcb_enter_notify_event_t*>(e);
enterNotifyEvent(event);
// MotionNotify is guaranteed to be generated only if the mouse
// move start and ends in the window; for cases when it only
// starts or only ends there, Enter/LeaveNotify are generated.
// Fake a MotionEvent in such cases to make handle of mouse
// events simpler (Qt does that too).
motionNotifyEvent(event->event, event->state,
event->event_x, event->event_y, event->root_x, event->root_y);
workspace()->updateFocusMousePosition(QPoint(event->root_x, event->root_y));
break;
}
case XCB_LEAVE_NOTIFY: {
auto *event = reinterpret_cast<xcb_leave_notify_event_t*>(e);
motionNotifyEvent(event->event, event->state,
event->event_x, event->event_y, event->root_x, event->root_y);
leaveNotifyEvent(event);
// not here, it'd break following enter notify handling
// workspace()->updateFocusMousePosition( QPoint( e->xcrossing.x_root, e->xcrossing.y_root ));
break;
}
case XCB_FOCUS_IN:
focusInEvent(reinterpret_cast<xcb_focus_in_event_t*>(e));
break;
case XCB_FOCUS_OUT:
focusOutEvent(reinterpret_cast<xcb_focus_out_event_t*>(e));
break;
case XCB_REPARENT_NOTIFY:
break;
case XCB_CLIENT_MESSAGE:
clientMessageEvent(reinterpret_cast<xcb_client_message_event_t*>(e));
break;
case XCB_EXPOSE: {
xcb_expose_event_t *event = reinterpret_cast<xcb_expose_event_t*>(e);
if (event->window == frameId() && !Compositor::self()->isActive()) {
// TODO: only repaint required areas
triggerDecorationRepaint();
}
break;
}
default:
if (eventType == Xcb::Extensions::self()->shapeNotifyEvent() && reinterpret_cast<xcb_shape_notify_event_t*>(e)->affected_window == window()) {
detectShape(window()); // workaround for #19644
updateShape();
}
if (eventType == Xcb::Extensions::self()->damageNotifyEvent() && reinterpret_cast<xcb_damage_notify_event_t*>(e)->drawable == frameId())
damageNotifyEvent();
break;
}
return true; // eat all events
}
/**
* Handles map requests of the client window
*/
bool X11Client::mapRequestEvent(xcb_map_request_event_t *e)
{
if (e->window != window()) {
// Special support for the save-set feature, which is a bit broken.
// If there's a window from one client embedded in another one,
// e.g. using XEMBED, and the embedder suddenly loses its X connection,
// save-set will reparent the embedded window to its closest ancestor
// that will remains. Unfortunately, with reparenting window managers,
// this is not the root window, but the frame (or in KWin's case,
// it's the wrapper for the client window). In this case,
// the wrapper will get ReparentNotify for a window it won't know,
// which will be ignored, and then it gets MapRequest, as save-set
// always maps. Returning true here means that Workspace::workspaceEvent()
// will handle this MapRequest and manage this window (i.e. act as if
// it was reparented to root window).
if (e->parent == wrapperId())
return false;
return true; // no messing with frame etc.
}
// also copied in clientMessage()
if (isMinimized())
unminimize();
if (isShade())
setShade(ShadeNone);
if (!isOnCurrentDesktop()) {
if (workspace()->allowClientActivation(this))
workspace()->activateClient(this);
else
demandAttention();
}
return true;
}
/**
* Handles unmap notify events of the client window
*/
void X11Client::unmapNotifyEvent(xcb_unmap_notify_event_t *e)
{
if (e->window != window())
return;
if (e->event != wrapperId()) {
// most probably event from root window when initially reparenting
bool ignore = true;
if (e->event == rootWindow() && (e->response_type & 0x80))
ignore = false; // XWithdrawWindow()
if (ignore)
return;
}
// check whether this is result of an XReparentWindow - client then won't be parented by wrapper
// in this case do not release the client (causes reparent to root, removal from saveSet and what not)
// but just destroy the client
Xcb::Tree tree(m_client);
xcb_window_t daddy = tree.parent();
if (daddy == m_wrapper) {
releaseWindow(); // unmapped from a regular client state
} else {
destroyClient(); // the client was moved to some other parent
}
}
void X11Client::destroyNotifyEvent(xcb_destroy_notify_event_t *e)
{
if (e->window != window())
return;
destroyClient();
}
/**
* Handles client messages for the client window
*/
void X11Client::clientMessageEvent(xcb_client_message_event_t *e)
{
Toplevel::clientMessageEvent(e);
if (e->window != window())
return; // ignore frame/wrapper
// WM_STATE
if (e->type == atoms->wm_change_state) {
if (e->data.data32[0] == XCB_ICCCM_WM_STATE_ICONIC)
minimize();
return;
}
}
/**
* Handles configure requests of the client window
*/
void X11Client::configureRequestEvent(xcb_configure_request_event_t *e)
{
if (e->window != window())
return; // ignore frame/wrapper
if (isResize() || isMove())
return; // we have better things to do right now
if (m_fullscreenMode == FullScreenNormal) { // refuse resizing of fullscreen windows
// but allow resizing fullscreen hacks in order to let them cancel fullscreen mode
sendSyntheticConfigureNotify();
return;
}
if (isSplash()) { // no manipulations with splashscreens either
sendSyntheticConfigureNotify();
return;
}
if (e->value_mask & XCB_CONFIG_WINDOW_BORDER_WIDTH) {
// first, get rid of a window border
m_client.setBorderWidth(0);
}
if (e->value_mask & (XCB_CONFIG_WINDOW_X | XCB_CONFIG_WINDOW_Y | XCB_CONFIG_WINDOW_HEIGHT | XCB_CONFIG_WINDOW_WIDTH))
configureRequest(e->value_mask, e->x, e->y, e->width, e->height, 0, false);
if (e->value_mask & XCB_CONFIG_WINDOW_STACK_MODE)
restackWindow(e->sibling, e->stack_mode, NET::FromApplication, userTime(), false);
// Sending a synthetic configure notify always is fine, even in cases where
// the ICCCM doesn't require this - it can be though of as 'the WM decided to move
// the window later'. The client should not cause that many configure request,
// so this should not have any significant impact. With user moving/resizing
// the it should be optimized though (see also X11Client::setGeometry()/plainResize()/move()).
sendSyntheticConfigureNotify();
// SELI TODO accept configure requests for isDesktop windows (because kdesktop
// may get XRANDR resize event before kwin), but check it's still at the bottom?
}
/**
* Handles property changes of the client window
*/
void X11Client::propertyNotifyEvent(xcb_property_notify_event_t *e)
{
Toplevel::propertyNotifyEvent(e);
if (e->window != window())
return; // ignore frame/wrapper
switch(e->atom) {
case XCB_ATOM_WM_NORMAL_HINTS:
getWmNormalHints();
break;
case XCB_ATOM_WM_NAME:
fetchName();
break;
case XCB_ATOM_WM_ICON_NAME:
fetchIconicName();
break;
case XCB_ATOM_WM_TRANSIENT_FOR:
readTransient();
break;
case XCB_ATOM_WM_HINTS:
getIcons(); // because KWin::icon() uses WMHints as fallback
break;
default:
if (e->atom == atoms->motif_wm_hints) {
getMotifHints();
} else if (e->atom == atoms->net_wm_sync_request_counter)
getSyncCounter();
else if (e->atom == atoms->activities)
checkActivities();
else if (e->atom == atoms->kde_first_in_window_list)
updateFirstInTabBox();
else if (e->atom == atoms->kde_color_sheme)
updateColorScheme();
else if (e->atom == atoms->kde_screen_edge_show)
updateShowOnScreenEdge();
else if (e->atom == atoms->kde_net_wm_appmenu_service_name)
checkApplicationMenuServiceName();
else if (e->atom == atoms->kde_net_wm_appmenu_object_path)
checkApplicationMenuObjectPath();
break;
}
}
void X11Client::enterNotifyEvent(xcb_enter_notify_event_t *e)
{
if (e->event != frameId())
return; // care only about entering the whole frame
#define MOUSE_DRIVEN_FOCUS (!options->focusPolicyIsReasonable() || \
(options->focusPolicy() == Options::FocusFollowsMouse && options->isNextFocusPrefersMouse()))
if (e->mode == XCB_NOTIFY_MODE_NORMAL || (e->mode == XCB_NOTIFY_MODE_UNGRAB && MOUSE_DRIVEN_FOCUS)) {
if (options->isShadeHover()) {
cancelShadeHoverTimer();
if (isShade()) {
shadeHoverTimer = new QTimer(this);
connect(shadeHoverTimer, SIGNAL(timeout()), this, SLOT(shadeHover()));
shadeHoverTimer->setSingleShot(true);
shadeHoverTimer->start(options->shadeHoverInterval());
}
}
#undef MOUSE_DRIVEN_FOCUS
enterEvent(QPoint(e->root_x, e->root_y));
return;
}
}
void X11Client::leaveNotifyEvent(xcb_leave_notify_event_t *e)
{
if (e->event != frameId())
return; // care only about leaving the whole frame
if (e->mode == XCB_NOTIFY_MODE_NORMAL) {
if (!isMoveResizePointerButtonDown()) {
setMoveResizePointerMode(PositionCenter);
updateCursor();
}
bool lostMouse = !rect().contains(QPoint(e->event_x, e->event_y));
// 'lostMouse' wouldn't work with e.g. B2 or Keramik, which have non-rectangular decorations
// (i.e. the LeaveNotify event comes before leaving the rect and no LeaveNotify event
// comes after leaving the rect) - so lets check if the pointer is really outside the window
// TODO this still sucks if a window appears above this one - it should lose the mouse
// if this window is another client, but not if it's a popup ... maybe after KDE3.1 :(
// (repeat after me 'AARGHL!')
if (!lostMouse && e->detail != XCB_NOTIFY_DETAIL_INFERIOR) {
Xcb::Pointer pointer(frameId());
if (!pointer || !pointer->same_screen || pointer->child == XCB_WINDOW_NONE) {
// really lost the mouse
lostMouse = true;
}
}
if (lostMouse) {
leaveEvent();
cancelShadeHoverTimer();
if (shade_mode == ShadeHover && !isMoveResize() && !isMoveResizePointerButtonDown()) {
shadeHoverTimer = new QTimer(this);
connect(shadeHoverTimer, SIGNAL(timeout()), this, SLOT(shadeUnhover()));
shadeHoverTimer->setSingleShot(true);
shadeHoverTimer->start(options->shadeHoverInterval());
}
if (isDecorated()) {
// sending a move instead of a leave. With leave we need to send proper coords, with move it's handled internally
QHoverEvent leaveEvent(QEvent::HoverMove, QPointF(-1, -1), QPointF(-1, -1), Qt::NoModifier);
QCoreApplication::sendEvent(decoration(), &leaveEvent);
}
}
if (options->focusPolicy() == Options::FocusStrictlyUnderMouse && isActive() && lostMouse) {
workspace()->requestDelayFocus(nullptr);
}
return;
}
}
#define XCapL KKeyServer::modXLock()
#define XNumL KKeyServer::modXNumLock()
#define XScrL KKeyServer::modXScrollLock()
void X11Client::grabButton(Qt::KeyboardModifier modifier, uint8_t button)
{
uint16_t x11Modifier;
switch (modifier) {
case Qt::MetaModifier:
x11Modifier = KKeyServer::modXMeta();
break;
case Qt::AltModifier:
x11Modifier = KKeyServer::modXAlt();
break;
default:
x11Modifier = 0;
break;
}
unsigned int mods[ 8 ] = {
0, XCapL, XNumL, XNumL | XCapL,
XScrL, XScrL | XCapL,
XScrL | XNumL, XScrL | XNumL | XCapL
};
for (int i = 0;
i < 8;
++i)
m_wrapper.grabButton(XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC, x11Modifier | mods[ i ], button);
}
#undef XCapL
#undef XNumL
#undef XScrL
bool X11Client::isMostRecentlyRaised() const
{
// The last toplevel in the unconstrained stacking order is the most recently raised one.
return workspace()->topClientOnDesktop(VirtualDesktopManager::self()->current(), -1, true, false) == this;
}
void X11Client::updateMouseGrab()
{
xcb_ungrab_button(connection(), XCB_BUTTON_INDEX_ANY, m_wrapper, XCB_MOD_MASK_ANY);
if (TabBox::TabBox::self()->forcedGlobalMouseGrab()) { // see TabBox::establishTabBoxGrab()
m_wrapper.grabButton(XCB_GRAB_MODE_SYNC, XCB_GRAB_MODE_ASYNC);
return;
}
// When a passive grab is activated or deactivated, the X server will generate crossing
// events as if the pointer were suddenly to warp from its current position to some position
// in the grab window. Some /broken/ X11 clients do get confused by such EnterNotify and
// LeaveNotify events so we release the passive grab for the active window.
//
// The passive grab below is established so the window can be raised or activated when it
// is clicked.
if ((options->focusPolicyIsReasonable() && !isActive()) ||
(options->isClickRaise() && !isMostRecentlyRaised())) {
if (options->commandWindow1() != Options::MouseNothing) {
grabButton(Qt::NoModifier, XCB_BUTTON_INDEX_1);
}
if (options->commandWindow2() != Options::MouseNothing) {
grabButton(Qt::NoModifier, XCB_BUTTON_INDEX_2);
}
if (options->commandWindow3() != Options::MouseNothing) {
grabButton(Qt::NoModifier, XCB_BUTTON_INDEX_3);
}
if (options->commandWindowWheel() != Options::MouseNothing) {
grabButton(Qt::NoModifier, XCB_BUTTON_INDEX_4);
grabButton(Qt::NoModifier, XCB_BUTTON_INDEX_5);
}
}
// We want to grab <command modifier> + buttons no matter what state the window is in. The
// client will receive funky EnterNotify and LeaveNotify events, but there is nothing that
// we can do about it, unfortunately.
if (!workspace()->globalShortcutsDisabled()) {
if (options->commandAll1() != Options::MouseNothing) {
grabButton(options->commandAllModifier(), XCB_BUTTON_INDEX_1);
}
if (options->commandAll2() != Options::MouseNothing) {
grabButton(options->commandAllModifier(), XCB_BUTTON_INDEX_2);
}
if (options->commandAll3() != Options::MouseNothing) {
grabButton(options->commandAllModifier(), XCB_BUTTON_INDEX_3);
}
if (options->commandAllWheel() != Options::MouseWheelNothing) {
grabButton(options->commandAllModifier(), XCB_BUTTON_INDEX_4);
grabButton(options->commandAllModifier(), XCB_BUTTON_INDEX_5);
}
}
}
static bool modKeyDown(int state) {
const uint keyModX = (options->keyCmdAllModKey() == Qt::Key_Meta) ?
KKeyServer::modXMeta() : KKeyServer::modXAlt();
return keyModX && (state & KKeyServer::accelModMaskX()) == keyModX;
}
// return value matters only when filtering events before decoration gets them
bool X11Client::buttonPressEvent(xcb_window_t w, int button, int state, int x, int y, int x_root, int y_root, xcb_timestamp_t time)
{
if (isMoveResizePointerButtonDown()) {
if (w == wrapperId())
xcb_allow_events(connection(), XCB_ALLOW_SYNC_POINTER, XCB_TIME_CURRENT_TIME); //xTime());
return true;
}
if (w == wrapperId() || w == frameId() || w == inputId()) {
// FRAME neco s tohohle by se melo zpracovat, nez to dostane dekorace
updateUserTime(time);
const bool bModKeyHeld = modKeyDown(state);
if (isSplash()
&& button == XCB_BUTTON_INDEX_1 && !bModKeyHeld) {
// hide splashwindow if the user clicks on it
hideClient(true);
if (w == wrapperId())
xcb_allow_events(connection(), XCB_ALLOW_SYNC_POINTER, XCB_TIME_CURRENT_TIME); //xTime());
return true;
}
Options::MouseCommand com = Options::MouseNothing;
bool was_action = false;
if (bModKeyHeld) {
was_action = true;
switch(button) {
case XCB_BUTTON_INDEX_1:
com = options->commandAll1();
break;
case XCB_BUTTON_INDEX_2:
com = options->commandAll2();
break;
case XCB_BUTTON_INDEX_3:
com = options->commandAll3();
break;
case XCB_BUTTON_INDEX_4:
case XCB_BUTTON_INDEX_5:
com = options->operationWindowMouseWheel(button == XCB_BUTTON_INDEX_4 ? 120 : -120);
break;
}
} else {
if (w == wrapperId()) {
if (button < 4) {
com = getMouseCommand(x11ToQtMouseButton(button), &was_action);
} else if (button < 6) {
com = getWheelCommand(Qt::Vertical, &was_action);
}
}
}
if (was_action) {
bool replay = performMouseCommand(com, QPoint(x_root, y_root));
if (isSpecialWindow())
replay = true;
if (w == wrapperId()) // these can come only from a grab
xcb_allow_events(connection(), replay ? XCB_ALLOW_REPLAY_POINTER : XCB_ALLOW_SYNC_POINTER, XCB_TIME_CURRENT_TIME); //xTime());
return true;