Skip to content

Commit

Permalink
add: mouse get and set for linux
Browse files Browse the repository at this point in the history
  • Loading branch information
islonely committed Feb 26, 2023
0 parents commit 221c21b
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions src/c/mouse.h
Original file line number Diff line number Diff line change
@@ -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;
};
30 changes: 30 additions & 0 deletions src/c/mouse_linux.c
Original file line number Diff line number Diff line change
@@ -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);
}
10 changes: 10 additions & 0 deletions src/c/mouse_linux.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <stdio.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#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);
16 changes: 16 additions & 0 deletions src/c/mouse_windows.c
Original file line number Diff line number Diff line change
@@ -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);
}
7 changes: 7 additions & 0 deletions src/c/mouse_windows.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <windows.h>
#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);
47 changes: 47 additions & 0 deletions src/main.v
Original file line number Diff line number Diff line change
@@ -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())
}
7 changes: 7 additions & 0 deletions v.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Module {
name: 'mouse'
description: 'Set and get mouse position.'
version: '0.1'
license: 'MIT'
dependencies: []
}

0 comments on commit 221c21b

Please sign in to comment.