diff --git a/src/mouse.c.v b/src/mouse.c.v index 092d7b2..124f5e7 100644 --- a/src/mouse.c.v +++ b/src/mouse.c.v @@ -2,15 +2,6 @@ module auto import time -#flag -I @VMODROOT/src/c -#include "mouse.h" -#flag linux -lX11 -#flag linux @VMODROOT/src/c/mouse_linux.o - -$if linux { - #include "mouse_linux.h" -} - // Button is the buttons on a mouse or touchpad. pub enum Button { left @@ -60,9 +51,19 @@ pub fn Mouse.get_pos() (int, int) { } return -1, -1 } $else { - pos := C.get_mouse_pos() - return pos.x, pos.y + if compositor == .x11 { + display := C.XOpenDisplay(0) + defer { _ := C.XCloseDisplay(display) } + window := C.XRootWindow(display, 0) + root_id, child_id := u32(0), u32(0) + win_x, win_y, mask := u32(0), u32(0), u32(0) + mut pos_x, mut pos_y := 0, 0 + C.XQueryPointer(display, window, &root_id, &child_id, &pos_x, &pos_y, &win_x, &win_y, &mask) + return pos_x, pos_y + } + println(compositor) } + return -1, -1 } // Mouse.get_pos_opt returns the global X and Y coordinates of the mouse cursor. @@ -86,7 +87,14 @@ pub fn Mouse.set_pos(x int, y int) { C.mouse_event(C.MOUSEEVENTF_ABSOLUTE | C.MOUSEEVENTF_MOVE, target_x, target_y, 0, 0) } $else { - C.set_mouse_pos(x, y) + if compositor == .x11 { + display := C.XOpenDisplay(0) + defer { _ := C.XCloseDisplay(display) } + window := C.XRootWindow(display, 0) + C.XSelectInput(display, window, C.KeyReleaseMask) + C.XWarpPointer(display, C.None, window, 0, 0, 0, 0, x, y) + C.XFlush(display) + } } } diff --git a/src/screen.c.v b/src/screen.c.v index d7525b3..5616198 100644 --- a/src/screen.c.v +++ b/src/screen.c.v @@ -27,11 +27,16 @@ pub fn Screen.compositor() Compositor { $if windows { return .windows } - _ := os.getenv_opt('WAYLAND_DISPLAY') or { - _ := os.getenv_opt('DISPLAY') or { return .unknown } + xdg_session_type := os.getenv_opt('XDG_SESSION_TYPE') or { + // assume X11 return .x11 } - return .wayland + return if xdg_session_type == 'wayland' { + .wayland + } else { + // assume X11 + .x11 + } } // Screen.size returns the size of the primary display. diff --git a/src/types.c.v b/src/types.c.v index f48b364..9489aae 100644 --- a/src/types.c.v +++ b/src/types.c.v @@ -1,7 +1,11 @@ module auto $if linux { + #flag -lX11 #flag -lXrandr + #include + #include + #include #include } $else $if windows { #flag -mwindows @@ -10,7 +14,7 @@ $if linux { #flag -framework ApplicationServices #include } $else { - $compile_error('unsupported OS') + $compile_error('Unsupported OS') } // Size is the width and height of a screen. @@ -47,6 +51,11 @@ struct C.XRRCrtcInfo { fn C.XOpenDisplay(int) voidptr fn C.XCloseDisplay(voidptr) int +fn C.XQueryPointer(voidptr, voidptr, &u32, &u32, &int, &int, &u32, &u32, &u32) +fn C.XRootWindow(voidptr, int) voidptr +fn C.XSelectInput(voidptr, voidptr, int) +fn C.XWarpPointer(voidptr, int, voidptr, int, int, int, int, int, int) +fn C.XFlush(voidptr) fn C.DefaultScreen(voidptr) int fn C.DefaultRootWindow(voidptr) u64 fn C.XRRGetScreenResources(voidptr, u64) &C.XRRScreenResources