Skip to content

Commit

Permalink
gtk2: backport some event handling patches fixing high CPU usage. Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lazka authored and Alexpux committed Nov 14, 2018
1 parent 0ac45eb commit 2cf301d
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 3 deletions.
@@ -0,0 +1,49 @@
From bfdac2f70e005b2504cc3f4ebbdab328974d005a Mon Sep 17 00:00:00 2001
From: Jeremy Tan <jtanx@outlook.com>
Date: Sat, 17 Sep 2016 17:19:59 +0800
Subject: [PATCH 06/25] GDK W32: Always process all available messages

The GLib main loop blocks on MsgWaitForMultipleObjectsEx to
determine if there are any incoming messages while also allowing
for background tasks to run. If all available messages are not
processed after MsgWaitForMultipleObjectsEx has signaled that
there are available, CPU usage will skyrocket.

From my limited understanding (by inspection of profiling
under Visual Studio):
Key is pressed - MsgWaitForMultipleObjectsEx unblocks, and
sends message to GDK's event handler. Some event is now queued.

g_poll unblocks, calls the g_event_dispatch which finally
resolves to gdk_event_dispatch. This then calls
_gdk_win32_display_queue_events, but since a message is already
queued, it fails to call PeekMessage and returns immediately.

At the next iteration, g_poll again calls MsgWaitForMultipleObjectsEx
which queues yet another event and returns almost immediately, since
there are events available which haven't been processed by PeekMessage.

The dispatch function is then called and the process repeats.

https://bugzilla.gnome.org/show_bug.cgi?id=771568
---
gdk/win32/gdkevents-win32.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/gdk/win32/gdkevents-win32.c b/gdk/win32/gdkevents-win32.c
index 9b09edd052..e52f21c52e 100644
--- a/gdk/win32/gdkevents-win32.c
+++ b/gdk/win32/gdkevents-win32.c
@@ -3622,8 +3622,7 @@ _gdk_events_queue (GdkDisplay *display)
if (modal_win32_dialog != NULL)
return;

- while (!_gdk_event_queue_find_first (display) &&
- PeekMessageW (&msg, NULL, 0, 0, PM_REMOVE))
+ while (PeekMessageW (&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage (&msg);
DispatchMessageW (&msg);
--
2.19.1

@@ -0,0 +1,36 @@
From 61162225f712df648f38fd12bc0817cfa9f79a64 Mon Sep 17 00:00:00 2001
From: Jeremy Tan <jtanx@outlook.com>
Date: Sat, 17 Sep 2016 20:46:30 +0800
Subject: [PATCH 07/25] GDK W32: Ignore autorepeated key presses on modifier
keys

The X11 backend does not send autorepeated messages for modifier keys,
and doing so prevents motion compression from working.

https://bugzilla.gnome.org/show_bug.cgi?id=771568
---
gdk/win32/gdkevents-win32.c | 8 ++++++++
1 file changed, 8 insertions(+)

diff --git a/gdk/win32/gdkevents-win32.c b/gdk/win32/gdkevents-win32.c
index e52f21c52e..ac91fa64cb 100644
--- a/gdk/win32/gdkevents-win32.c
+++ b/gdk/win32/gdkevents-win32.c
@@ -2274,6 +2274,14 @@ gdk_event_translate (MSG *msg,
in_ime_composition)
break;

+ /* Ignore autorepeats on modifiers */
+ if (msg->message == WM_KEYDOWN &&
+ (msg->wParam == VK_MENU ||
+ msg->wParam == VK_CONTROL ||
+ msg->wParam == VK_SHIFT) &&
+ ((HIWORD(msg->lParam) & KF_REPEAT) >= 1))
+ break;
+
if (!propagate (&window, msg,
_gdk_display->keyboard_grab.window,
_gdk_display->keyboard_grab.owner_events,
--
2.19.1

13 changes: 10 additions & 3 deletions mingw-w64-gtk2/PKGBUILD
Expand Up @@ -5,7 +5,7 @@ _realname=gtk2
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.24.32
pkgrel=2
pkgrel=3
pkgdesc="GTK+ is a multi-platform toolkit (v2) (mingw-w64)"
arch=('any')
url="https://www.gtk.org/"
Expand Down Expand Up @@ -40,7 +40,9 @@ source=("https://download.gnome.org/sources/gtk+/${pkgver%.*}/gtk+-${pkgver}.tar
0011-gir-for-gdkwin32.mingw.patch
0012-embed-manifest.all.patch
0013_fix_mouse_events.patch
0019_use_g_stat_and_gstatbuf.patch)
0019_use_g_stat_and_gstatbuf.patch
0020-GDK-W32-Always-process-all-available-messages.patch
0021-GDK-W32-Ignore-autorepeated-key-presses-on-modifier-.patch)

sha256sums=('b6c8a93ddda5eabe3bfee1eb39636c9a03d2a56c7b62828b359bf197943c582e'
'b77a427df55a14182c10ad7e683b4d662df2846fcd38df2aa8918159d6be3ae2'
Expand All @@ -53,7 +55,9 @@ sha256sums=('b6c8a93ddda5eabe3bfee1eb39636c9a03d2a56c7b62828b359bf197943c582e'
'10569e2bc2afe6ab320dcb4b0e708ddd8235244ec0daff05ca518cb8463f2924'
'4bc70de68084585dcf2b5a7e4abe32a789360fc2be97a9c94ba3b52dac89ad93'
'f6d731acecf6e35e4ec9739a6d949d2d045c9396c4564283ee6ec935ec690e24'
'2903708fda7b9f306a6a7b7b41518ce7a9ac7b9e622d4a1eed9ce30b945d5971')
'2903708fda7b9f306a6a7b7b41518ce7a9ac7b9e622d4a1eed9ce30b945d5971'
'f984baf140fa325fab1bbd213d17f9cdcd5138b5ab9d76bd17c2434c4e4b2ac5'
'8955a689e9107c175a79766d4dd485941b3974b181e51f40ed2ae6028fd1b170')

prepare() {
cd gtk+-${pkgver}
Expand All @@ -68,6 +72,9 @@ prepare() {
patch -p1 -i ${srcdir}/0013_fix_mouse_events.patch
patch -p1 -i ${srcdir}/0019_use_g_stat_and_gstatbuf.patch

patch -p1 -i ${srcdir}/0020-GDK-W32-Always-process-all-available-messages.patch
patch -p1 -i ${srcdir}/0021-GDK-W32-Ignore-autorepeated-key-presses-on-modifier-.patch

autoreconf -fi
rm "${srcdir}/gtk+-${pkgver}/gtk/gtk.def"
}
Expand Down

0 comments on commit 2cf301d

Please sign in to comment.