From 6fb0e2781e895f7235983224f8e00f502a79b7a2 Mon Sep 17 00:00:00 2001 From: XProger Date: Tue, 6 Mar 2018 03:50:23 +0300 Subject: [PATCH] #15 Linux vibration improvements; #11 turn off gamepad vibration for inventory and loading screen --- bin/.gitignore | 9 +++++ src/camera.h | 4 +-- src/game.h | 3 +- src/input.h | 9 +++-- src/inventory.h | 2 ++ src/lara.h | 4 +-- src/platform/nix/main.cpp | 72 ++++++++++++++++++--------------------- src/utils.h | 7 ++-- 8 files changed, 62 insertions(+), 48 deletions(-) create mode 100644 bin/.gitignore diff --git a/bin/.gitignore b/bin/.gitignore new file mode 100644 index 0000000..facd744 --- /dev/null +++ b/bin/.gitignore @@ -0,0 +1,9 @@ +*.PHD +*.ogg +*.PSX +*.PNG +*.PCX +*.RAW +*.zip +*.txt + diff --git a/src/camera.h b/src/camera.h index ab025cb..cf78536 100644 --- a/src/camera.h +++ b/src/camera.h @@ -345,7 +345,7 @@ struct Camera : ICamera { virtual void update() { if (shake > 0.0f) { shake = max(0.0f, shake - Core::deltaTime); - Input::setJoyVibrate(cameraIndex, clamp(shake, 0.0f, 1.0f), 0); + Input::setJoyVibration(cameraIndex, clamp(shake, 0.0f, 1.0f), 0); } if (mode == MODE_CUTSCENE) { @@ -562,4 +562,4 @@ struct Camera : ICamera { } }; -#endif \ No newline at end of file +#endif diff --git a/src/game.h b/src/game.h index 1e12036..74b16f6 100644 --- a/src/game.h +++ b/src/game.h @@ -14,6 +14,7 @@ namespace Game { Stream *nextLevel; void startLevel(Stream *lvl) { + Input::stopJoyVibration(); delete level; level = new Level(*lvl); UI::game = level; @@ -179,4 +180,4 @@ namespace Game { } } -#endif \ No newline at end of file +#endif diff --git a/src/input.h b/src/input.h index 93153ed..7671b38 100644 --- a/src/input.h +++ b/src/input.h @@ -117,11 +117,16 @@ namespace Input { setJoyDown(index, key, pos.x > 0.0f); // gamepad LT, RT auto-down state } - void setJoyVibrate(int playerIndex, float L, float R) { + void setJoyVibration(int playerIndex, float L, float R) { if (!Core::settings.controls[playerIndex].vibration) return; osJoyVibrate(Core::settings.controls[playerIndex].joyIndex, L, R); } + + void stopJoyVibration() { + osJoyVibrate(Core::settings.controls[0].joyIndex, 0.0f, 0.0f); + osJoyVibrate(Core::settings.controls[1].joyIndex, 0.0f, 0.0f); + } InputKey getTouch(int id) { for (int i = 0; i < COUNT(touch); i++) @@ -284,4 +289,4 @@ namespace Input { } } -#endif \ No newline at end of file +#endif diff --git a/src/inventory.h b/src/inventory.h index 87d5c98..d293c31 100644 --- a/src/inventory.h +++ b/src/inventory.h @@ -647,6 +647,8 @@ struct Inventory { } bool toggle(int playerIndex = 0, Page curPage = PAGE_INVENTORY, TR::Entity::Type type = TR::Entity::LARA) { + Input::stopJoyVibration(); + this->playerIndex = playerIndex; titleTimer = 0.0f; diff --git a/src/lara.h b/src/lara.h index c87d4be..8ffe252 100644 --- a/src/lara.h +++ b/src/lara.h @@ -2733,9 +2733,9 @@ struct Lara : Character { if (hitTimer > 0.0f) { hitTimer -= Core::deltaTime; if (hitTimer > 0.0f) - Input::setJoyVibrate(camera->cameraIndex, 0.5f, 0.5f); + Input::setJoyVibration(camera->cameraIndex, 0.5f, 0.5f); else - Input::setJoyVibrate(camera->cameraIndex, 0, 0); + Input::setJoyVibration(camera->cameraIndex, 0, 0); } if (level->isCutsceneLevel()) diff --git a/src/platform/nix/main.cpp b/src/platform/nix/main.cpp index dad45f3..19f1528 100644 --- a/src/platform/nix/main.cpp +++ b/src/platform/nix/main.cpp @@ -139,7 +139,6 @@ void* sndFill(void *arg) { } void sndInit() { -return; static const pa_sample_spec spec = { .format = PA_SAMPLE_S16LE, .rate = 44100, @@ -198,15 +197,18 @@ InputKey mouseToInputKey(int btn) { return ikNone; } +#define JOY_DEAD_ZONE_STICK 8192 +#define JOY_DEAD_ZONE_TRIGGER 8192 +#define JOY_MIN_UPDATE_FX_TIME 50.0f + struct JoyDevice { - int fd; - int fe; - vec2 L, R; - ff_effect fx; - char event[128]; - int time; - - float vL, vR, oL, oR; + int fd; // device file descriptor + int fe; // event file descriptor + vec2 L, R; // left/right stick axes values + float vL, vR; // current value for left/right motor vibration + float oL, oR; // last applied value + int time; // time when we can send effect update + ff_effect fx; // effect structure } joyDevice[INPUT_JOY_COUNT]; bool osJoyReady(int index) { @@ -219,10 +221,6 @@ void osJoyVibrate(int index, float L, float R) { joyDevice[index].vR = R; } -#define JOY_DEAD_ZONE_STICK 8192 -#define JOY_DEAD_ZONE_TRIGGER 8192 -#define TEST_BIT(arr, bit) ((arr[bit / 32] >> (bit % 32)) & 1) - void joyInit() { LOG("init gamepads:\n"); char name[128]; @@ -261,8 +259,8 @@ void joyInit() { DIR *dir = opendir(name); if (!dir) continue; closedir(dir); - sprintf(joy.event, "/dev/input/event%d", j); - joy.fe = open(joy.event, O_RDWR); + sprintf(name, "/dev/input/event%d", j); + joy.fe = open(name, O_RDWR); break; } @@ -273,11 +271,16 @@ void joyInit() { } if (joy.fe > -1) { - LOG(" vibration feature\n"); - joy.fx.id = -1; - joy.time = osGetTime(); + int n_effects; + if (ioctl(joy.fe, EVIOCGEFFECTS, &n_effects) == -1) { + perror("Ioctl query"); + } + LOG(" vibration feature %d\n", n_effects); + joy.fx.id = -1; + joy.fx.type = FF_RUMBLE; + joy.fx.replay.delay = 0; joy.vL = joy.oL = joy.vR = joy.oR = 0.0f; - close(joy.fe); + joy.time = osGetTime(); } } } @@ -316,40 +319,33 @@ void joyRumble(JoyDevice &joy) { if (osGetTime() < joy.time) return; - - close(joy.fe); - joy.fe = open(joy.event, O_RDWR); - if (joy.fe == -1) - LOG("can't open event\n"); input_event event; event.type = EV_FF; - if (joy.oL != 0.0f || joy.oR != 0.0f) { - event.value = 0; - event.code = joy.fx.id; - if (write(joy.fe, &event, sizeof(event)) == -1) - LOG("! joy stop fx\n"); - } - if (joy.vL != 0.0f || joy.vR != 0.0f) { - joy.fx.type = FF_RUMBLE; + // update effect joy.fx.u.rumble.strong_magnitude = int(joy.vL * 65535); joy.fx.u.rumble.weak_magnitude = int(joy.vR * 65535); - joy.fx.replay.length = int(max(50.0f, 1000.0f / Core::stats.fps)); - joy.fx.replay.delay = 0; - joy.fx.id = -1; + joy.fx.replay.length = int(max(JOY_MIN_UPDATE_FX_TIME, 1000.0f / Core::stats.fps)); if (ioctl(joy.fe, EVIOCSFF, &joy.fx) == -1) { - LOG("! joy drv send fx\n"); - return; + LOG("! joy update fx\n"); } + // play effect event.value = 1; event.code = joy.fx.id; if (write(joy.fe, &event, sizeof(event)) == -1) LOG("! joy play fx\n"); - } + } else + if (joy.oL != 0.0f || joy.oR != 0.0f) { + // stop effect + event.value = 0; + event.code = joy.fx.id; + if (write(joy.fe, &event, sizeof(event)) == -1) + LOG("! joy stop fx\n"); + } joy.oL = joy.vL; joy.oR = joy.vR; diff --git a/src/utils.h b/src/utils.h index c298bd1..37e0938 100644 --- a/src/utils.h +++ b/src/utils.h @@ -73,10 +73,11 @@ typedef unsigned char uint8; typedef unsigned short uint16; typedef unsigned int uint32; -#define FOURCC(str) uint32(((uint8*)(str))[0] | (((uint8*)(str))[1] << 8) | (((uint8*)(str))[2] << 16) | (((uint8*)(str))[3] << 24) ) +#define FOURCC(str) uint32(((uint8*)(str))[0] | (((uint8*)(str))[1] << 8) | (((uint8*)(str))[2] << 16) | (((uint8*)(str))[3] << 24) ) -#define COUNT(arr) (sizeof(arr) / sizeof(arr[0])) -#define OFFSETOF(T, E) ((size_t)&(((T*)0)->E)) +#define COUNT(arr) (sizeof(arr) / sizeof(arr[0])) +#define OFFSETOF(T, E) ((size_t)&(((T*)0)->E)) +#define TEST_BIT(arr, bit) ((arr[bit / 32] >> (bit % 32)) & 1) template inline const T& min(const T &a, const T &b) {