Skip to content

Commit

Permalink
add Mouse cursor tracking (Linux)
Browse files Browse the repository at this point in the history
  • Loading branch information
gewang committed Nov 20, 2024
1 parent 9c05525 commit 468d0e3
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/core/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ ifneq ($(CHUCK_STRICT),)
CFLAGS+= -Wall
endif

# use X11? (Linux)
ifneq ($(USE_X11),)
CFLAGS+= -D__USE_X11__
LDFLAGS+= -lX11
endif

# ifneq ($(findstring arm,$(shell uname -m)),)
# some sort of arm platform- enable aggressive optimizations
# CFLAGS+= -ffast-math
Expand Down
69 changes: 68 additions & 1 deletion src/core/util_hid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6387,6 +6387,11 @@ Linux general HID support
#include <linux/joystick.h>
#include <linux/input.h>

#if defined(__USE_X11__)
// for Mouse | 1.5.4.2 (ge) added
#include <X11/Xlib.h>
#endif

#define CK_HID_DIR ("/dev/input")
#define CK_HID_MOUSEFILE ("mouse%d")
#define CK_HID_JOYSTICKFILE ("js%d")
Expand Down Expand Up @@ -7892,6 +7897,37 @@ t_CKVEC2 ck_get_mouse_xy_normalize()
// t_CKFLOAT screenWidth = GetSystemMetrics( SM_CXVIRTUALSCREEN );
// t_CKFLOAT screenHeight = GetSystemMetrics( SM_CYVIRTUALSCREEN );
#elif defined(__PLATFORM_LINUX__)

#if defined(__USE_X11__)
// stuff to query
Window root;
XWindowAttributes attributes;
int qRootX, qRootY, qChildX, qChildY;
Window qRoot, qChild;
unsigned int qMask;
// open connection to X server
Display * display = XOpenDisplay( nullptr );
if( !display )
{
// print error
std::cerr << "Mouse cannot open display (X server)..." << std::endl;
goto done;
}
// get root window
root = DefaultRootWindow( display );
// get the point
XQueryPointer( display, root, &qRoot, &qChild, &qRootX, &qRootY, &qChildX, &qChildY, &qMask );
// get window attributes
XGetWindowAttributes( display, root, &attributes );
// copy to return value
retval.x = (t_CKFLOAT)qRootX / attributes.width;
retval.y = (t_CKFLOAT)qRootY / attributes.height;
// close the connection
XCloseDisplay(display);
#else // no __USE_X11__
cerr << "Mouse needs X11 on Linux; to use, re-compile chuck with USE_X11=1" << endl;
#endif // if defined(__USE_X11__)

#else
// unsupported, for now; return 0,0
#endif
Expand Down Expand Up @@ -7925,8 +7961,39 @@ t_CKVEC2 ck_get_mouse_xy_absolute()
retval.y = pt.y;
}
#elif defined(__PLATFORM_LINUX__)

#if defined(__USE_X11__)
// stuff to query
Window root;
int qRootX, qRootY, qChildX, qChildY;
Window qRoot, qChild;
unsigned int qMask;
// open connection to X server
Display * display = XOpenDisplay( nullptr );
if( !display )
{
// print error
std::cerr << "Mouse cannot open display (X server)..." << std::endl;
goto done;
}
// get root window
root = DefaultRootWindow( display );
// get the point
XQueryPointer( display, root, &qRoot, &qChild, &qRootX, &qRootY, &qChildX, &qChildY, &qMask );
// copy to return value
retval.x = qRootX;
retval.y = qRootY;
// close the connection
XCloseDisplay(display);
#else // no __USE_X11__
cerr << "Mouse needs X11 on Linux; to use, re-compile chuck with USE_X11=1" << endl;
#endif // if defined(__USE_X11__)

#else
#endif
// unsupported platform; nothing to do for now; return 0,0
#endif // platform select

done:
return retval;
}

Expand Down
6 changes: 6 additions & 0 deletions src/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ ifneq ($(CHUCK_STRICT),)
CFLAGS+= -Wall
endif

# use X11? (Linux)
ifneq ($(USE_X11),)
CFLAGS+= -D__USE_X11__
LDFLAGS+= -lX11
endif

# ifneq ($(findstring arm,$(shell uname -m)),)
# some sort of arm platform- enable aggressive optimizations
# CFLAGS+= -ffast-math
Expand Down

0 comments on commit 468d0e3

Please sign in to comment.