Skip to content

Commit

Permalink
linux: mouse get and set fns
Browse files Browse the repository at this point in the history
  • Loading branch information
islonely committed Jan 17, 2025
1 parent c98db47 commit 8c4043f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
32 changes: 20 additions & 12 deletions src/mouse.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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)
}
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/screen.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 10 additions & 1 deletion src/types.c.v
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
module auto

$if linux {
#flag -lX11
#flag -lXrandr
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/extensions/Xrandr.h>
} $else $if windows {
#flag -mwindows
Expand All @@ -10,7 +14,7 @@ $if linux {
#flag -framework ApplicationServices
#include <ApplicationServices/ApplicationServices.h>
} $else {
$compile_error('unsupported OS')
$compile_error('Unsupported OS')
}

// Size is the width and height of a screen.
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8c4043f

Please sign in to comment.