$NetBSD$

--- CMakeLists.txt.orig	2018-05-16 02:48:18.000000000 +0000
+++ CMakeLists.txt
@@ -163,6 +163,8 @@ if (UNIX)
             link_directories("/usr/local/lib")
         endif()
 
+	find_package(X11 REQUIRED)
+
         set (XKBlib "X11/Xlib.h;X11/XKBlib.h")
         set (CMAKE_EXTRA_INCLUDE_FILES "${XKBlib};X11/extensions/Xrandr.h")
         check_type_size ("XRRNotifyEvent" X11_EXTENSIONS_XRANDR_H)
@@ -178,27 +180,17 @@ if (UNIX)
         check_include_files ("X11/extensions/XInput2.h" HAVE_XI2)
         check_include_files ("dns_sd.h" HAVE_DNSSD)
 
-        if (HAVE_X11_EXTENSIONS_DPMS_H)
+        if (X11_dpms_FOUND)
             # Assume that function prototypes declared, when include exists.
             set (HAVE_DPMS_PROTOTYPES 1)
         endif()
 
-        if (NOT HAVE_X11_XKBLIB_H)
-            message (FATAL_ERROR "Missing header: " ${XKBlib})
-        endif()
-
+	check_include_files ("dns_sd.h" HAVE_DNSSD)
         if (NOT HAVE_DNSSD)
             message (FATAL_ERROR "Missing header: dns_sd.h")
         endif()
 
-        check_library_exists ("SM;ICE" IceConnectionNumber "" HAVE_ICE)
-        check_library_exists ("Xext;X11" DPMSQueryExtension "" HAVE_Xext)
-        check_library_exists ("Xtst;Xext;X11" XTestQueryExtension "" HAVE_Xtst)
-        check_library_exists ("Xinerama" XineramaQueryExtension "" HAVE_Xinerama)
-        check_library_exists ("Xi" XISelectEvents "" HAVE_Xi)
-        check_library_exists ("Xrandr" XRRQueryExtension "" HAVE_Xrandr)
-
-        if (HAVE_ICE)
+        if (X11_ICE_FOUND)
 
             # Assume we have SM if we have ICE.
             set (HAVE_SM 1)
@@ -206,39 +198,39 @@ if (UNIX)
 
         endif()
 
-        if (HAVE_Xtst)
+        if (X11_XTest_FOUND)
 
-            # Xtxt depends on X11.
-            set (HAVE_X11 1)
-            list (APPEND libs Xtst X11)
+	    message ("APPENDING ${X11_XTest_LIB}")
+            list (APPEND libs ${X11_XTest_LIB} X11)
 
         else()
 
-            message (FATAL_ERROR "Missing library: Xtst")
+            message (FATAL_ERROR "Missing library: Xtest")
 
         endif()
 
-        if (HAVE_Xext)
-            list (APPEND libs Xext)
+        if (X11_Xext_FOUND)
+	    message ("APPENDING ${X11_Xext_LIB}")
+            list (APPEND libs ${X11_Xext_LIB})
         endif()
 
-        if (HAVE_Xinerama)
-            list (APPEND libs Xinerama)
-        else (HAVE_Xinerama)
-            if (HAVE_X11_EXTENSIONS_XINERAMA_H)
-                set (HAVE_X11_EXTENSIONS_XINERAMA_H 0)
-                message (WARNING "Old Xinerama implementation detected, disabled")
-            endif()
+        if (X11_Xinerama_FOUND)
+            list (APPEND libs ${X11_Xinerama_LIB})
+        else (X11_Xinerama_FOUND)
+            set (HAVE_X11_EXTENSIONS_XINERAMA_H 0)
+            message (WARNING "No Xinerama implementation detected, disabled")
         endif()
 
-        if (HAVE_Xrandr)
-            list (APPEND libs Xrandr)
+        if (X11_Xrandr_FOUND)
+	    message ("APPENDING ${X11_Xrandr_LIB}")
+            list (APPEND libs ${X11_Xrandr_LIB})
         endif()
 
         # this was outside of the linux scope,
         # not sure why, moving it back inside.
-        if (HAVE_Xi)
-            list (APPEND libs Xi)
+        if (X11_Xi_FOUND)
+	    message ("APPENDING ${X11_Xi_FOUND}")
+            list (APPEND libs ${X11_Xi_LIB})
         endif()
 
     endif()
