Created
October 1, 2012 23:38
-
-
Save dlion/3815207 to your computer and use it in GitHub Desktop.
Simple snippet to move the pointer on the screen using a joystick
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*############################################################################### | |
*# @Author : Domenico Luciani aka DLion | |
*# @Description: Simple snippet for move the pointer on the screen using a joystick | |
*# @How compile: g++ <source> -o <binary> -lplibjs -lplibsl -lplibsm -lplibul -lm -lX11 | |
*# @Copyright : 2012 | |
*# @Site : http://www.about.me/DLion | |
*# @License : GNU AGPL v3 http://www.gnu.org/licenses/agpl.html | |
*###############################################################################*/ | |
#include <X11/Xlib.h> | |
#include <X11/X.h> | |
#include <X11/Xutil.h> | |
#include <plib/js.h> | |
int main() | |
{ | |
Display *monitor; | |
Window win; | |
jsJoystick *js[1]; | |
float *ax[1]; | |
int x=0,y=0,b; | |
//Init plib | |
jsInit(); | |
//get first joystick | |
js[0] = new jsJoystick(); | |
//Check if the joystick is present | |
if(js[0]->notWorking()) | |
printf("Joystick not detected!\n"); | |
else | |
{ | |
//Get axes | |
ax[0] = (float*)malloc((js[0]->getNumAxes())*sizeof(float)); | |
//Functions for X | |
monitor = XOpenDisplay(0); | |
win = XRootWindow(monitor,0); | |
XSelectInput(monitor,win,NoEventMask); | |
while(1) | |
{ | |
//Get Button pressed and Axes | |
js[0]->read(&b,ax[0]); | |
//Increment o decrement x and y axes | |
x+=ax[0][0]; | |
y+=ax[0][1]; | |
//Move pointer on the screen | |
XWarpPointer(monitor,None,win,0,0,0,0,x,y); | |
XFlush(monitor); | |
//Sleep 1 second | |
usleep(1000); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment