jangxx / node-vigemclient Goto Github PK
View Code? Open in Web Editor NEWNative Node.js bindings for the ViGEm virtual gamepad driver
License: MIT License
Native Node.js bindings for the ViGEm virtual gamepad driver
License: MIT License
when trying to disconnect a connected controller, the application crashes, for example:
const ViGEmClient = require("vigemclient");
let client = new ViGEmClient();
client.connect();
let controller = client.createX360Controller();
controller.connect();
setTimeout(()=>{
controller.disconnect();
},1000)
causes the terminal to spit out:
npm[16920]: c:\ws\src\node_api.cc:1075: Assertion `(func) != nullptr' failed.
1: 00007FF71140A41F v8::internal::wasm::DisjointAllocationPool::~DisjointAllocationPool+74447
2: 00007FF7113B2F26 v8::base::CPU::has_sse+59718
3: 00007FF7113B32A1 v8::base::CPU::has_sse+60609
4: 00007FF7113DA8BE napi_call_threadsafe_function+30
5: 00007FFEAFF6FB9F x360_notification_callback+159 [D:\Downloads\node-ViGEmClient-master\node-ViGEmClient-master\src\target_x360.cpp]:L9
6: 00007FFEB7BDE988 vigem_target_x360_get_user_index+3816
7: 00007FFEB7BDE823 vigem_target_x360_get_user_index+3459
8: 00007FFEB7BD4D41
9: 00007FFEB7BD46B5
10: 00007FFEB7BD41CA
11: 00007FFEB7BD5563
12: 00007FFEB7BE0A73 vigem_target_x360_get_user_index+12243
13: 00007FFEB7C04F10 vigem_target_x360_get_user_index+160880
14: 00007FFECF037BD4 BaseThreadInitThunk+20
15: 00007FFED018CE51 RtlUserThreadStart+33
at first I thought it because controller does not unregister the notification receiver, so i tried the following:
const ViGEmClient = require(".."); // lets say this is done from example folder in repo
const vigemclient = require('../build/Release/vigemclient');
let client = new ViGEmClient();
client.connect();
let controller = client.createX360Controller();
controller.connect();
setTimeout(() => {
vigemclient.vigem_target_x360_unregister_notification(controller._target);
controller.disconnect();
}, 1000);
this causes the application to close with:
error Command failed with exit code 3221225477.
or when run with npx:
/d/Programs/Programming/NodeJS/npx: line 35: 2976 Segmentation fault "$NODE_EXE" "$NPX_CLI_JS" "$@"
I tried putting a delay between removing the notification and disconnecting the controller, and it seems to work, but its not consistent;
const ViGEmClient = require("..");
const vigemclient = require("../build/Release/vigemclient");
let client = new ViGEmClient();
client.connect();
let controller = client.createX360Controller();
controller.connect();
setTimeout(() => {
vigemclient.vigem_target_x360_unregister_notification(controller._target);
setTimeout(() => {
controller.disconnect();
}, 100);
}, 1000);
Hello again, as you suggested I switched to using ViGEm :)
This is working perfectly, except I can't figure out how to enable/disable the left an right triggers on an xbox controller? How can I do this?
I've tried using LEFT/RIGHT_SHOULDER
and LEFT/RIGHT_THUMB
to no avail.
When setting the USB VID and PID, the VID and PID don't change in Device Manager, I'm guessing this is an issue with my code?
Code:
client.connect();
let controller = client.createX360Controller();
controller.connect();
controller.vendorID = 0x1BAD;
controller.productID = 0x0003;
Files are installed and built correctly yet it is not able to resolve the built files. I'm assuming its the vigemclient.node file it's looking for, and I can see it in ../build/Release/. Any solutions on how to resolve this issue?
`[ERROR] Could not resolve "../build/Release/vigemclient"
node_modules/vigemclient/lib/ViGEmClient.js:1:28:
1 │ const vigemclient = require('../build/Release/vigemclient');
╵ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
node_modules/vigemclient/lib/X360Controller.js:1:28:
1 │ const vigemclient = require('../build/Release/vigemclient');
╵ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
node_modules/vigemclient/lib/DS4Controller.js:1:28:
1 │ const vigemclient = require('../build/Release/vigemclient');
╵ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
node_modules/vigemclient/lib/ViGEmTarget.js:3:28:
3 │ const vigemclient = require('../build/Release/vigemclient');
╵ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
So, I've been working on making a virtual gamepad, but when I'm making it, there are some issues I've found on the making
When creating the controller, the axis value will not be exactly zero
I Believe the left analog axis is error, when it's the first value that changed and the analog is in the first quadrant ( 0 to 90 degrees ) and the third quadrant ( 180 - 270 degrees ). The right analog is just fine. It will look like this
Note: I've tried another tester. I've verified my input value. And etc. And in conclusion, I thought it's probably in the library itself.
The Code ( Bear with my code, lol ) :
`const feeder = require('./vigem');
var controllers = {};
const express = require('express');
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
const port = process.env.PORT || 7200;
const buttons = {
BOTTOM_BUTTON : "A",
RIGHT_BUTTON : "B",
TOP_BUTTON : "Y",
LEFT_BUTTON : "X",
RIGHT_SHOULDER : "RIGHT_SHOULDER",
LEFT_SHOULDER : "LEFT_SHOULDER",
START : "START",
BACK : "BACK"
}
const axes = {
ANALOG_LEFT : "left",
ANALOG_RIGHT: "right"
}
const shoulder = {
LEFT_TRIGGER : "left",
RIGHT_TRIGGER: "right"
}
// Set root to xbox layout
app.get('/', (req, res) => {
return res.sendFile(__dirname + "/static/s-new.html");
});
// Set /static as root so the assets and stuffs can be loaded
app.use('/', express.static(__dirname + '/static'));
// Socket.io
io.on('connection', (socket) => {
let notificationCallback = function(data)
{
if(data)
{
io.to(socket.id).emit("vibration", true);
} else {
io.to(socket.id).emit("vibration", false);
}
}
controllers[socket.id] = new feeder("x360", notificationCallback);
let client = controllers[socket.id];
// console.log(`a user connected with socketID of ${socket.id}`);
// // Sending to all client
// socket.emit("message", "can you hear me?");
// // Sending to specific client
// io.to(socket.id).emit("message", "I just met you");
socket.on('disconnect', () => {
console.log('user disconnected');
client.disconnect();
});
socket.on('message', (tes) => {
switch(tes.inputType)
{
case "axis":
if(tes.axis == "ANALOG_LEFT" || tes.axis == "ANALOG_RIGHT")
{
tes.x = client.clampIt(tes.x, tes.r)
tes.y = client.clampIt(tes.y, tes.r)
console.log(tes.x, tes.y)
client.xbox360SetAxisValue(`${axes[tes.axis]}X`, tes.x );
client.xbox360SetAxisValue(`${axes[tes.axis]}Y`, tes.y );
client.update();
}
break;
case "triggerAxis":
if(tes.axis == "LEFT_TRIGGER" || tes.axis == "RIGHT_TRIGGER")
{
tes.value = tes.value/tes.max;
client.xbox360SetAxisValue(`${shoulder[tes.axis]}Trigger`, tes.value );
client.update();
}
break;
case "button":
client.xbox360SetButtonValue(buttons[tes.button], !!tes.v);
break;
}
//console.log(`${JSON.stringify(tes)}`);
});
});
http.listen(port, () => {
console.log(Listenting on ${port}
);
});`
`const ViGEmClient = require('vigemclient');
class feeder {
constructor(controller, notificationCallback)
{
switch(controller) {
case "x360":
this._controllerName = "x360";
this.xbox360feeder(notificationCallback);
// this.xbox360reset();
break;
case "ds4":
this._controllerName = "ds4";
this.ds4feeder(notificationCallback);
break;
default:
this._controllerName = "x360";
this.xbox360feeder(notificationCallback);
}
}
connectClient()
{
this.client = new ViGEmClient();
let err = this.client.connect(); // establish connection to the ViGEmBus driver
return err;
}
connectController(controllerName)
{
switch(controllerName) {
case "x360":
this.controller = this.client.createX360Controller(); //Spawn x360 virtual controller
break;
case "ds4":
this.controller = this.client.createDS4Controller(); //Spawn ds4 virtual controller
break;
default:
this.controller = this.client.createX360Controller(); //Spawn x360 virtual controller
}
let err = this.controller.connect();
return err;
}
outputIDs(controllerName)
{
console.log("Vendor ID:", this.controller.vendorID);
console.log("Product ID:", this.controller.productID);
console.log("Index:", this.controller.index);
console.log("Type:", this.controller.type);
// if(controllerName == "x360")
// {
// console.log("User index:", this.controller.userIndex);
// }
}
clamp(value, min, max) {
return Math.min(max, Math.max(min, value));
}
update()
{
this.controller.update(); // update manually for better performance
}
disconnect()
{
let err = this.controller.disconnect(); // update manually for better performance
return err;
}
convertAngleToAxis(a, s) // To convert Angle and Strength of a circle to X and Y of square
{
let x = (s/100)*Math.cos(a*Math.PI/180);
let y = (s/100)*Math.sin(a*Math.PI/180);
let xSquare = 0.5*Math.sqrt(2+2*Math.sqrt(2)*x+Math.pow(x,2)-Math.pow(y,2)) - 0.5*Math.sqrt(2-2*Math.sqrt(2)*x+Math.pow(x,2)-Math.pow(y,2)); // x = ½ √( 2 + 2u√2 + u² - v² ) - ½ √( 2 - 2u√2 + u² - v² )
let ySquare = 0.5*Math.sqrt(2+2*Math.sqrt(2)*y-Math.pow(x,2)+Math.pow(y,2)) - 0.5*Math.sqrt(2-2*Math.sqrt(2)*y-Math.pow(x,2)+Math.pow(y,2)); // y = ½ √( 2 + 2v√2 - u² + v² ) - ½ √( 2 - 2v√2 - u² + v² )
if(isNaN(xSquare) && isNaN(ySquare)) {
if(x>=0)
{
xSquare = 1;
} else {
xSquare = -1;
}
if(y>=0)
{
ySquare = 1;
} else {
ySquare = -1;
}
}
return {x: xSquare, y: ySquare};
}
convertCircleCoordToSquareCoord(u, v)
{
u = u / 35 * 1; // 35 = radius of the joystick // u,v element u^2 + v^2 <= 1
v = -v / 35 * 1; // 35 = radius of the joystick // u,v element u^2 + v^2 <= 1 // The y is inverted, from the client so we need to invert it here
let xSquare = 0.5*Math.sqrt(2+2*Math.sqrt(2)*u+Math.pow(u,2)-Math.pow(v,2)) - 0.5*Math.sqrt(2-2*Math.sqrt(2)*u+Math.pow(u,2)-Math.pow(v,2)); // x = ½ √( 2 + 2u√2 + u² - v² ) - ½ √( 2 - 2u√2 + u² - v² )
let ySquare = 0.5*Math.sqrt(2+2*Math.sqrt(2)*v-Math.pow(u,2)+Math.pow(v,2)) - 0.5*Math.sqrt(2-2*Math.sqrt(2)*v-Math.pow(u,2)+Math.pow(v,2)); // y = ½ √( 2 + 2v√2 - u² + v² ) - ½ √( 2 - 2v√2 - u² + v² )
if(isNaN(xSquare) && isNaN(ySquare)) {
if(u>=0)
{
xSquare = 1;
} else {
xSquare = -1;
}
if(v>=0)
{
ySquare = 1;
} else {
ySquare = -1;
}
}
return {x: xSquare, y: ySquare};
}
clampIt(value, max)
{
return value/max;
}
//--------------------------------------------------[ XBOX 360 Feeder ]--------------------------------------------------//
xbox360reset()
{
let value = 0;
this.controller.button.X.setValue(value); // press X button
this.controller.button.Y.setValue(value); // press Y button
this.controller.button.A.setValue(value); // press A button
this.controller.button.B.setValue(value); // press B button
// this.controller.button.DPAD_UP.setValue(value); // press DPAD_UP button
// this.controller.button.DPAD_DOWN.setValue(value); // press DPAD_DOWN button
// this.controller.button.DPAD_LEFT.setValue(value); // press DPAD_LEFT button
// this.controller.button.DPAD_RIGHT.setValue(value); // press DPAD_RIGHT button
this.controller.button.START.setValue(value); // press START button
this.controller.button.BACK.setValue(value); // press BACK button
this.controller.button.LEFT_THUMB.setValue(value); // press LEFT_THUMB button
this.controller.button.RIGHT_THUMB.setValue(value); // press RIGHT_THUMB button
this.controller.button.LEFT_SHOULDER.setValue(value); // press LEFT_SHOULDER button
this.controller.button.RIGHT_SHOULDER.setValue(value); // press RIGHT_SHOULDER button
this.controller.button.GUIDE.setValue(value); // press GUIDE button
this.controller.axis.leftX.setValue(value); // Left analog X
this.controller.axis.leftY.setValue(value); // Left analog Y
this.controller.axis.leftTrigger.setValue(value); // Left Trigger
this.controller.axis.rightX.setValue(value); // Right analog X
this.controller.axis.rightY.setValue(value); // Right analog Y
this.controller.axis.rightTrigger.setValue(value); // Right Trigger
this.controller.axis.dpadHorz.setValue(value); // Horizontal DPAD
this.controller.axis.dpadVert.setValue(value); // Horizontal DPAD
}
xbox360feeder(notificationCallback)
{
let connectClient = this.connectClient(); // Connecting to ViGEmBus driver
if(connectClient == null) // Checking if connectclient returning null which what we wanted
{
let errConnectController = this.connectController(this._controllerName); // Connecting to virtual controller
if (errConnectController) // Checking if errconnectcontroller returning true which is error happenned
{
console.log(errConnectController.message); // Outputting the error message
process.exit(1);
}
this.outputIDs(this._controllerName); // Outputting vendor id and stuffs
// Controller notification
this.controller.on("notification", data => {
console.log("notification", data);
// if(data.LargeMotor > 0 || data.SmallMotor > 0)
// {
// notificationCallback(true);
// } else {
// notificationCallback(false);
// }
});
// this.controller.updateMode = "manual"; // requires manual calls to controller.update()
let tes = 0;
let testing = setInterval(function()
{
if(tes%2 == 0)
{
notificationCallback(true);
} else {
notificationCallback(false);
}
if(tes == 3){
clearInterval(testing);
}
tes++;
}, 10000);
}
}
xbox360SetButtonValue(buttonName, value)
{
let err = null;
if(value !== true && value !== false)
{
err = true;
}
if(!err)
{
switch(buttonName) {
case "X":
this.controller.button.X.setValue(value); // press X button
break;
case "Y":
this.controller.button.Y.setValue(value); // press Y button
break;
case "A":
this.controller.button.A.setValue(value); // press A button
break;
case "B":
this.controller.button.B.setValue(value); // press B button
break;
// case "DPAD_UP":
// this.controller.button.DPAD_UP.setValue(value); // press DPAD_UP button
// break;
// case "DPAD_DOWN":
// this.controller.button.DPAD_DOWN.setValue(value); // press DPAD_DOWN button
// break;
// case "DPAD_LEFT":
// this.controller.button.DPAD_LEFT.setValue(value); // press DPAD_LEFT button
// break;
// case "DPAD_RIGHT":
// this.controller.button.DPAD_RIGHT.setValue(value); // press DPAD_RIGHT button
// break;
case "START":
this.controller.button.START.setValue(value); // press START button
break;
case "BACK":
this.controller.button.BACK.setValue(value); // press BACK button
break;
case "LEFT_THUMB":
this.controller.button.LEFT_THUMB.setValue(value); // press LEFT_THUMB button
break;
case "RIGHT_THUMB":
this.controller.button.RIGHT_THUMB.setValue(value); // press RIGHT_THUMB button
break;
case "LEFT_SHOULDER":
this.controller.button.LEFT_SHOULDER.setValue(value); // press LEFT_SHOULDER button
break;
case "RIGHT_SHOULDER":
this.controller.button.RIGHT_SHOULDER.setValue(value); // press RIGHT_SHOULDER button
break;
case "GUIDE":
this.controller.button.GUIDE.setValue(value); // press GUIDE button
break;
default:
err = false;
}
}
if(this.controller.updateMode == "manual")
{
this.update();
}
return err;
}
xbox360ButtonToggle(index)
{
let err = null;
let buttons = Object.keys(this.controller.button);
if(index > buttons.length)
{
err = true;
}
if(!err)
{
this.controller.button[buttons[index]].setValue(!this.controller.button[buttons[index]].value); // invert button value
}
this.update();
return err;
}
xbox360SetAxisValue(axisName, value)
{
let err = null;
if((value == null) && (typeof(value) == "number"))
{
err = true;
}
if(!err)
{
switch(axisName)
{
case "leftX":
this.controller.axis.leftX.setValue(value); // Left analog X
break;
case "leftY":
this.controller.axis.leftY.setValue(value); // Left analog Y
break;
case "leftTrigger":
this.controller.axis.leftTrigger.setValue(value); // Left Trigger
break;
case "rightX":
this.controller.axis.rightX.setValue(value); // Right analog X
break;
case "rightY":
this.controller.axis.rightY.setValue(value); // Right analog Y
break;
case "rightTrigger":
this.controller.axis.rightTrigger.setValue(value); // Right Trigger
break;
case "dpadHorz":
this.controller.axis.dpadHorz.setValue(value); // Horizontal DPAD
break;
case "dpadVert":
this.controller.axis.dpadVert.setValue(value); // Horizontal DPAD
break;
default:
err = false;
}
}
if(this.controller.updateMode == "manual")
{
this.update();
}
return err;
}
//--------------------------------------------------[ XBOX 360 Feeder ]--------------------------------------------------//
}
module.exports = feeder;`
example code returns ViGEmTarget but actual code returns X360Controller.
so I'm having to call _axes etc.
This is my code :
`
xbox360SetButtonValue(buttonName, value)
{
let err = null;
if(value !== true && value !== false)
{
err = true;
}
if(!err)
{
switch(buttonName) {
case "X":
this.controller.button.X.setValue(value); // press X button
console.log(`${buttonName}`)
break;
case "Y":
this.controller.button.Y.setValue(value); // press Y button
console.log(`${buttonName}`)
break;
case "A":
this.controller.button.A.setValue(value); // press A button
console.log(`${buttonName}`)
break;
case "B":
this.controller.button.B.setValue(value); // press B button
console.log(`${buttonName}`)
break;
// case "DPAD_UP":
// this.controller.button.DPAD_UP.setValue(value); // press DPAD_UP button
// break;
// case "DPAD_DOWN":
// this.controller.button.DPAD_DOWN.setValue(value); // press DPAD_DOWN button
// break;
// case "DPAD_LEFT":
// this.controller.button.DPAD_LEFT.setValue(value); // press DPAD_LEFT button
// break;
// case "DPAD_RIGHT":
// this.controller.button.DPAD_RIGHT.setValue(value); // press DPAD_RIGHT button
// break;
case "START":
this.controller.button.START.setValue(value); // press START button
break;
case "BACK":
this.controller.button.BACK.setValue(value); // press BACK button
break;
case "LEFT_THUMB":
this.controller.button.LEFT_THUMB.setValue(value); // press LEFT_THUMB button
break;
case "RIGHT_THUMB":
this.controller.button.RIGHT_THUMB.setValue(value); // press RIGHT_THUMB button
break;
case "LEFT_SHOULDER":
this.controller.button.LEFT_SHOULDER.setValue(value); // press LEFT_SHOULDER button
break;
case "RIGHT_SHOULDER":
this.controller.button.RIGHT_SHOULDER.setValue(value); // press RIGHT_SHOULDER button
break;
case "GUIDE":
this.controller.button.GUIDE.setValue(value); // press GUIDE button
break;
default:
err = false;
}
}
return err;
}
`
My code is simple, yet the game or gamepad tester doesn't recognize the input. What did i do wrong ?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.