diff --git a/src/display-x11.cc b/src/display-x11.cc
--- a/src/display-x11.cc
+++ b/src/display-x11.cc
@@ -24,7 +24,6 @@
  *
  */
 
-#include <X11/extensions/XI2.h>
 #include <config.h>
 
 #ifdef BUILD_X11
@@ -47,6 +46,7 @@
 #include "mouse-events.h"
 #ifdef BUILD_XINPUT
 #include <X11/extensions/XInput2.h>
+#include <X11/extensions/XI2.h>
 #endif /* BUILD_XINPUT */
 #endif /* BUILD_MOUSE_EVENTS */
 #endif /* BUILD_X11 */
