Skip to content

Commit 20e00f0

Browse files
authored
Fix friendly params (#17)
* feat: be nice about wrong params * fix: don't throw exceptions for wrong arg type. Issue warning instead. * fix: update release notes Signed-off-by: David Nixon <[email protected]>
1 parent d80a3da commit 20e00f0

File tree

9 files changed

+141
-80
lines changed

9 files changed

+141
-80
lines changed

.idea/.gitignore

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/sdl2-gamecontroller.iml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

RELEASENOTES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# v1.0.8
2+
- Issue warning for mismatch params instead of throwing error. Resolve issue where a param is passed to `rumble` or another function but the param was undefined and an error is thrown. This was particularly an issue for the player number which is often not supported.
3+
14
# v1.0.7
25
- Add support for ASUS ROG Chakram mouse. (Requires SDL2 2.0.22)
36
- Add support for setting the polling interval

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sdl2-gamecontroller",
3-
"version": "1.0.7",
3+
"version": "1.0.8",
44
"scripts": {
55
"install": "cmake-js compile",
66
"lint": "prettier --write *.js *.ts test/*js test/*.ts",

src/sdlgamecontroller.cpp

Lines changed: 85 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -492,25 +492,26 @@ void SdlGameController::enableGyroscope(const Napi::CallbackInfo &info) {
492492

493493
SDL_bool enable = SDL_TRUE;
494494
int playerNumber = 0; // enable for all players
495+
auto warning = Napi::Object::New(env);
495496

496497
// Check if enable is set.
497498
if (info.Length() > 0) {
498-
// Check the argument types
499-
if (!info[0].IsBoolean()) {
500-
throw Napi::Error::New(env, "Wrong arguments");
501-
return;
499+
if (info[0].IsBoolean()) {
500+
enable = info[0].ToBoolean() ? SDL_TRUE : SDL_FALSE;
501+
} else {
502+
warning.Set("message", "wrong argument type: enable");
503+
emit({Napi::String::New(env, "warning"), warning});
502504
}
503-
enable = info[0].ToBoolean() ? SDL_TRUE : SDL_FALSE;
504505
}
505506

506507
// Check if player number is set
507508
if (info.Length() > 1) {
508-
// Check the argument types
509-
if (!info[0].IsNumber()) {
510-
throw Napi::Error::New(env, "Wrong arguments");
511-
return;
509+
if (info[0].IsNumber()) {
510+
playerNumber = info[1].ToNumber();
511+
} else {
512+
warning.Set("message", "wrong argument type: player");
513+
emit({Napi::String::New(env, "warning"), warning});
512514
}
513-
playerNumber = info[1].ToNumber();
514515
}
515516

516517
for (auto &controller : gamecontrollers) {
@@ -555,24 +556,25 @@ void SdlGameController::enableAccelerometer(const Napi::CallbackInfo &info) {
555556

556557
int playerNumber = 0; // enable for all players
557558
SDL_bool enable = SDL_TRUE;
559+
auto warning = Napi::Object::New(env);
558560

559561
// Check the number of arguments passed.
560562
if (info.Length() > 0) {
561-
// Check the argument types
562-
if (!info[0].IsBoolean()) {
563-
throw Napi::Error::New(env, "Wrong arguments");
564-
return;
563+
if (info[0].IsBoolean()) {
564+
enable = info[0].ToBoolean() ? SDL_TRUE : SDL_FALSE;
565+
} else {
566+
warning.Set("message", "wrong argument type: enable");
567+
emit({Napi::String::New(env, "warning"), warning});
565568
}
566-
enable = info[0].ToBoolean() ? SDL_TRUE : SDL_FALSE;
567569
}
568570

569571
if (info.Length() > 1) {
570-
// Check the argument types
571-
if (!info[1].IsNumber()) {
572-
throw Napi::Error::New(env, "Wrong arguments");
573-
return;
572+
if (info[1].IsNumber()) {
573+
playerNumber = info[1].ToNumber();
574+
} else {
575+
warning.Set("message", "wrong argument type: player");
576+
emit({Napi::String::New(env, "warning"), warning});
574577
}
575-
playerNumber = info[1].ToNumber();
576578
}
577579

578580
for (auto &controller : gamecontrollers) {
@@ -620,45 +622,46 @@ void SdlGameController::rumble(const Napi::CallbackInfo &info) {
620622
Uint16 low_frequency_rumble = 0xFFFC;
621623
Uint16 high_frequency_rumble = 0xFFFC;
622624
Uint32 duration_ms = 250;
625+
auto warning = Napi::Object::New(env);
623626

624627
// Check for low frequency
625628
if (info.Length() > 0) {
626-
// Check the argument type
627-
if (!info[0].IsNumber()) {
628-
throw Napi::Error::New(env, "Wrong arguments");
629-
return;
629+
if (info[0].IsNumber()) {
630+
low_frequency_rumble = static_cast<uint32_t>(info[0].ToNumber());
631+
} else {
632+
warning.Set("message", "wrong argument type: low_frequency_rumble");
633+
emit({Napi::String::New(env, "warning"), warning});
630634
}
631-
low_frequency_rumble = static_cast<uint32_t>(info[0].ToNumber());
632635
}
633636

634637
// Check for high frequency
635638
if (info.Length() > 1) {
636-
// Check the argument type
637-
if (!info[1].IsNumber()) {
638-
throw Napi::Error::New(env, "Wrong arguments");
639-
return;
639+
if (info[1].IsNumber()) {
640+
high_frequency_rumble = static_cast<uint32_t>(info[1].ToNumber());
641+
} else {
642+
warning.Set("message", "wrong argument type: high_frequency_rumble");
643+
emit({Napi::String::New(env, "warning"), warning});
640644
}
641-
high_frequency_rumble = static_cast<uint32_t>(info[1].ToNumber());
642645
}
643646

644647
// Check for high frequency
645648
if (info.Length() > 2) {
646-
// Check the argument type
647-
if (!info[2].IsNumber()) {
648-
throw Napi::Error::New(env, "Wrong arguments");
649-
return;
649+
if (info[2].IsNumber()) {
650+
duration_ms = static_cast<uint32_t>(info[2].ToNumber());
651+
} else {
652+
warning.Set("message", "wrong argument type: duration_ms");
653+
emit({Napi::String::New(env, "warning"), warning});
650654
}
651-
duration_ms = static_cast<uint32_t>(info[2].ToNumber());
652655
}
653656

654657
// Check for player number
655658
if (info.Length() > 3) {
656-
// Check the argument types
657-
if (!info[3].IsNumber()) {
658-
throw Napi::Error::New(env, "Wrong arguments");
659-
return;
659+
if (info[3].IsNumber()) {
660+
playerNumber = info[3].ToNumber();
661+
} else {
662+
warning.Set("message", "wrong argument type: player");
663+
emit({Napi::String::New(env, "warning"), warning});
660664
}
661-
playerNumber = info[3].ToNumber();
662665
}
663666

664667
for (auto &controller : gamecontrollers) {
@@ -705,45 +708,47 @@ void SdlGameController::rumbleTriggers(const Napi::CallbackInfo &info) {
705708
Uint16 left_rumble = 0xFFFC;
706709
Uint16 right_rumble = 0xFFFC;
707710
Uint32 duration_ms = 250;
711+
auto warning = Napi::Object::New(env);
708712

709713
// Check for left
710714
if (info.Length() > 0) {
711-
// Check the argument type
712-
if (!info[0].IsNumber()) {
713-
throw Napi::Error::New(env, "Wrong arguments");
714-
return;
715+
if (info[0].IsNumber()) {
716+
left_rumble = static_cast<uint32_t>(info[0].ToNumber());
717+
} else {
718+
warning.Set("message", "wrong argument type: left_rumble");
719+
emit({Napi::String::New(env, "warning"), warning});
715720
}
716-
left_rumble = static_cast<uint32_t>(info[0].ToNumber());
717721
}
718722

719723
// Check for right
720724
if (info.Length() > 1) {
721-
// Check the argument type
722-
if (!info[1].IsNumber()) {
723-
throw Napi::Error::New(env, "Wrong arguments");
724-
return;
725+
if (info[1].IsNumber()) {
726+
right_rumble = static_cast<uint32_t>(info[1].ToNumber());
727+
} else {
728+
warning.Set("message", "wrong argument type: right_rumble");
729+
emit({Napi::String::New(env, "warning"), warning});
725730
}
726-
right_rumble = static_cast<uint32_t>(info[1].ToNumber());
727731
}
728732

729733
// Check for duration
730734
if (info.Length() > 2) {
731-
// Check the argument type
732-
if (!info[2].IsNumber()) {
733-
throw Napi::Error::New(env, "Wrong arguments");
734-
return;
735+
if (info[2].IsNumber()) {
736+
duration_ms = static_cast<uint32_t>(info[2].ToNumber());
737+
} else {
738+
warning.Set("message", "wrong argument type: duration_ms");
739+
emit({Napi::String::New(env, "warning"), warning});
735740
}
736-
duration_ms = static_cast<uint32_t>(info[2].ToNumber());
737741
}
738742

739743
// Check for player number
740744
if (info.Length() > 3) {
741745
// Check the argument types
742-
if (!info[3].IsNumber()) {
743-
throw Napi::Error::New(env, "Wrong arguments");
744-
return;
746+
if (info[3].IsNumber()) {
747+
playerNumber = info[3].ToNumber();
748+
} else {
749+
warning.Set("message", "wrong argument type: player");
750+
emit({Napi::String::New(env, "warning"), warning});
745751
}
746-
playerNumber = info[3].ToNumber();
747752
}
748753

749754
for (auto &controller : gamecontrollers) {
@@ -790,45 +795,46 @@ void SdlGameController::setLeds(const Napi::CallbackInfo &info) {
790795
Uint8 red = 0x00;
791796
Uint8 green = 0x00;
792797
Uint8 blue = 0xff;
798+
auto warning = Napi::Object::New(env);
793799

794800
// Check for red
795801
if (info.Length() > 0) {
796-
// Check the argument type
797-
if (!info[0].IsNumber()) {
798-
throw Napi::Error::New(env, "Wrong arguments");
799-
return;
802+
if (info[0].IsNumber()) {
803+
red = static_cast<uint32_t>(info[0].ToNumber());
804+
} else {
805+
warning.Set("message", "wrong argument type: red");
806+
emit({Napi::String::New(env, "warning"), warning});
800807
}
801-
red = static_cast<uint32_t>(info[0].ToNumber());
802808
}
803809

804810
// Check for green
805811
if (info.Length() > 1) {
806-
// Check the argument type
807-
if (!info[1].IsNumber()) {
808-
throw Napi::Error::New(env, "Wrong arguments");
809-
return;
812+
if (info[1].IsNumber()) {
813+
green = static_cast<uint32_t>(info[1].ToNumber());
814+
} else {
815+
warning.Set("message", "wrong argument type: green");
816+
emit({Napi::String::New(env, "warning"), warning});
810817
}
811-
green = static_cast<uint32_t>(info[1].ToNumber());
812818
}
813819

814820
// Check for blue
815821
if (info.Length() > 2) {
816-
// Check the argument type
817-
if (!info[2].IsNumber()) {
818-
throw Napi::Error::New(env, "Wrong arguments");
819-
return;
822+
if (info[2].IsNumber()) {
823+
blue = static_cast<uint32_t>(info[2].ToNumber());
824+
} else {
825+
warning.Set("message", "wrong argument type: blue");
826+
emit({Napi::String::New(env, "warning"), warning});
820827
}
821-
blue = static_cast<uint32_t>(info[2].ToNumber());
822828
}
823829

824830
// Check for player number
825831
if (info.Length() > 3) {
826-
// Check the argument types
827-
if (!info[3].IsNumber()) {
828-
throw Napi::Error::New(env, "Wrong arguments");
829-
return;
832+
if (info[3].IsNumber()) {
833+
playerNumber = info[3].ToNumber();
834+
} else {
835+
warning.Set("message", "wrong argument type: player");
836+
emit({Napi::String::New(env, "warning"), warning});
830837
}
831-
playerNumber = info[3].ToNumber();
832838
}
833839

834840
for (auto &controller : gamecontrollers) {

test/arguments.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const gamecontroller = require('../index');
2+
3+
gamecontroller.on('error', (data) => console.log('error', data));
4+
gamecontroller.on('warning', (data) => console.log('warning', data));
5+
gamecontroller.on('sdl-init', (data) => console.log('SDL2 Initialized', data));
6+
gamecontroller.on('a:down', () => console.log('Hello A button world'));
7+
gamecontroller.on('controller-device-added', (data) => {
8+
console.log('controller connected', data.name);
9+
gamecontroller.rumble("bad",{bad:true}, false, undefined);
10+
gamecontroller.rumbleTriggers("bad",{bad:true}, false, undefined);
11+
gamecontroller.enableGyroscope(1, null);
12+
gamecontroller.enableAccelerometer(0, "one");
13+
gamecontroller.setLeds(undefined, false, "blue", "");
14+
process.exit(0);
15+
});

0 commit comments

Comments
 (0)