From 221c21bca58f0ba11a8b0be5acec3670f94f7fc0 Mon Sep 17 00:00:00 2001 From: Adam Oates <31167933+islonely@users.noreply.github.com> Date: Sun, 26 Feb 2023 08:56:07 -0600 Subject: [PATCH] add: mouse get and set for linux --- .editorconfig | 9 +++++++++ .gitattributes | 7 +++++++ .gitignore | 24 ++++++++++++++++++++++ src/c/mouse.h | 5 +++++ src/c/mouse_linux.c | 30 +++++++++++++++++++++++++++ src/c/mouse_linux.h | 10 +++++++++ src/c/mouse_windows.c | 16 +++++++++++++++ src/c/mouse_windows.h | 7 +++++++ src/main.v | 47 +++++++++++++++++++++++++++++++++++++++++++ v.mod | 7 +++++++ 10 files changed, 162 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 src/c/mouse.h create mode 100644 src/c/mouse_linux.c create mode 100644 src/c/mouse_linux.h create mode 100644 src/c/mouse_windows.c create mode 100644 src/c/mouse_windows.h create mode 100644 src/main.v create mode 100644 v.mod diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..517d63e --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.v] +indent_style = tab +indent_size = 4 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..f4011a7 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,7 @@ +* text=auto eol=lf +*.bat eol=crlf + +**/*.v linguist-language=V +**/*.vv linguist-language=V +**/*.vsh linguist-language=V +**/v.mod linguist-language=V diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8ce9ff7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,24 @@ +# Binaries for programs and plugins +main +mouse +*.exe +*.exe~ +*.so +*.dylib +*.dll + +# Ignore binary output folders +bin/ + +# Ignore common editor/system specific metadata +.DS_Store +.idea/ +.vscode/ +*.iml + +# ENV +.env + +# vweb and database +*.db +*.js diff --git a/src/c/mouse.h b/src/c/mouse.h new file mode 100644 index 0000000..ee8993d --- /dev/null +++ b/src/c/mouse.h @@ -0,0 +1,5 @@ +// Position is a point on an X, Y axis used to +// represent the location of the mouse pointer. +struct Position { + int x, y; +}; \ No newline at end of file diff --git a/src/c/mouse_linux.c b/src/c/mouse_linux.c new file mode 100644 index 0000000..038c75b --- /dev/null +++ b/src/c/mouse_linux.c @@ -0,0 +1,30 @@ +// See .h for comments. +#include "mouse_linux.h" + +struct Position get_mouse_pos() { + struct Position pos = {-1, -1}; + + Display *dpy; + Window root_window; + int root_id = 0, + child_id = 0, + win_x = 0, + win_y = 0, + mask = 0; + dpy = XOpenDisplay(0); + root_window = XRootWindow(dpy, 0); + XQueryPointer(dpy, root_window, &root_id, &child_id, &pos.x, &pos.y, &win_x, &win_y, &mask); + + return pos; +} + +void set_mouse_pos(int x, int y) { + Display *dpy; + Window root_window; + + dpy = XOpenDisplay(0); + root_window = XRootWindow(dpy, 0); + XSelectInput(dpy, root_window, KeyReleaseMask); + XWarpPointer(dpy, None, root_window, 0, 0, 0, 0, x, y); + XFlush(dpy); +} \ No newline at end of file diff --git a/src/c/mouse_linux.h b/src/c/mouse_linux.h new file mode 100644 index 0000000..382f62e --- /dev/null +++ b/src/c/mouse_linux.h @@ -0,0 +1,10 @@ +#include +#include +#include +#include +#include "mouse.h" + +// get_mouse_pos gets the mouse position and returns it. +struct Position get_mouse_pos(); +// set_mouse_pos moves the position of the mouse to X, Y. +void set_mouse_pos(int x, int y); \ No newline at end of file diff --git a/src/c/mouse_windows.c b/src/c/mouse_windows.c new file mode 100644 index 0000000..cc51c2a --- /dev/null +++ b/src/c/mouse_windows.c @@ -0,0 +1,16 @@ +// See .h for comments. +#include "mouse_windows.h" + +struct Position get_mouse_pos() { + struct Position pos = {-1, -1}; + + + + return pos; +} + +void set_mouse_pos(int x, int y) { + // int nX = x * 65535 / GetSystemMetrics(SM_CXSCREEN); + // int nY = y * 65535 / GetSystemMetrics(SM_CYSCREEN); + // mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE, nX, nY, 0, 0); +} \ No newline at end of file diff --git a/src/c/mouse_windows.h b/src/c/mouse_windows.h new file mode 100644 index 0000000..657fb63 --- /dev/null +++ b/src/c/mouse_windows.h @@ -0,0 +1,7 @@ +#include +#include "mouse.h" + +// get_mouse_pos gets the mouse position and returns it. +struct Position get_mouse_pos(); +// set_mouse_pos moves the position of the mouse to X, Y. +void set_mouse_pos(int x, int y); \ No newline at end of file diff --git a/src/main.v b/src/main.v new file mode 100644 index 0000000..a17f955 --- /dev/null +++ b/src/main.v @@ -0,0 +1,47 @@ +module main + +#flag -I @VMODROOT/src/c +#flag linux -lX11 +#flag linux @VMODROOT/src/c/mouse_linux.o +#flag windows @VMODROOT/src/c/mouse_windows.o + +$if linux { + #include "mouse_linux.h" +} $else $if windows { + #include "mouse_windows.h" +} $else $if macos { + +} + +// See .c files for comments. +struct C.Position { + x int + y int +} + +fn C.get_mouse_pos() C.Position +fn C.set_mouse_pos(int, int) + +[inline] +pub fn get() (int, int) { + pos := C.get_mouse_pos() + return pos.x, pos.y +} + +[inline] +pub fn get_opt() ?(int, int) { + x, y := get() + if -1 in [x, y] { + return none + } + return x, y +} + +[inline] +pub fn set(x int, y int) { + C.set_mouse_pos(x, y) +} + +fn main() { + println(get()) +} diff --git a/v.mod b/v.mod new file mode 100644 index 0000000..876b307 --- /dev/null +++ b/v.mod @@ -0,0 +1,7 @@ +Module { + name: 'mouse' + description: 'Set and get mouse position.' + version: '0.1' + license: 'MIT' + dependencies: [] +}