Improve joystick button reset procedure

This commit is contained in:
Aaron Culliney 2019-11-10 16:10:08 -08:00
parent 5aa691815b
commit 397b43b3e7
7 changed files with 33 additions and 25 deletions

View File

@ -291,7 +291,7 @@ void Java_org_deadc0de_apple2ix_Apple2View_nativeRender(JNIEnv *env, jclass cls)
void Java_org_deadc0de_apple2ix_Apple2Activity_nativeReboot(JNIEnv *env, jclass cls, jint resetState) { void Java_org_deadc0de_apple2ix_Apple2Activity_nativeReboot(JNIEnv *env, jclass cls, jint resetState) {
LOG("..."); LOG("...");
if (resetState) { if (resetState) {
// joystick button settings should be balanced by c_joystick_reset() triggered on CPU thread // joystick button settings should be balanced by joystick_reset() triggered on CPU thread
if (resetState == 1) { if (resetState == 1) {
run_args.joy_button0 = 0xff; run_args.joy_button0 = 0xff;
run_args.joy_button1 = 0x0; run_args.joy_button1 = 0x0;

View File

@ -662,7 +662,7 @@ void cpu65_uninterrupt(int reason) {
} }
void cpu65_reboot(void) { void cpu65_reboot(void) {
run_args.joy_button0 = 0xff; // OpenApple -- should be balanced by c_joystick_reset() triggers on CPU thread run_args.joy_button0 = 0xff; // OpenApple -- should be balanced by joystick_reset() triggers on CPU thread
cpu65_interrupt(ResetSig); cpu65_interrupt(ResetSig);
} }

View File

@ -1089,7 +1089,7 @@ void c_interface_parameters()
else if ((ch == kESC) || c_keys_is_interface_key(ch)) else if ((ch == kESC) || c_keys_is_interface_key(ch))
{ {
timing_initialize(); timing_initialize();
c_joystick_reset(); joystick_reset();
#if !TESTING #if !TESTING
prefs_save(); prefs_save();
#endif #endif
@ -1183,7 +1183,7 @@ void c_interface_parameters()
/* calibrate joystick */ /* calibrate joystick */
if ((ch == 13) && (option == OPT_CALIBRATE)) if ((ch == 13) && (option == OPT_CALIBRATE))
{ {
c_joystick_reset(); joystick_reset();
c_calibrate_joystick(); c_calibrate_joystick();
c_interface_print_screen( screen ); c_interface_print_screen( screen );
} }
@ -1246,7 +1246,7 @@ void c_interface_parameters()
ch = toupper(ch); ch = toupper(ch);
if (ch == 'Y') if (ch == 'Y')
{ {
c_joystick_reset(); joystick_reset();
cpu65_reboot(); cpu65_reboot();
c_interface_exit(ch); c_interface_exit(ch);
break; break;

View File

@ -32,6 +32,11 @@ short joy_step = 1;
bool joy_auto_recenter = false; bool joy_auto_recenter = false;
#endif #endif
static struct {
video_frame_callback_fn frameCallback;
uint8_t frameCount;
} joystickReset = { 0 };
void (*joydriver_resetJoystick)(void) = NULL; void (*joydriver_resetJoystick)(void) = NULL;
static void joystick_prefsChanged(const char *domain) { static void joystick_prefsChanged(const char *domain) {
@ -59,8 +64,13 @@ static void joystick_prefsChanged(const char *domain) {
#endif #endif
} }
static __attribute__((constructor)) void _init_joystick(void) { static void _init_joystick(void) {
prefs_registerListener(PREF_DOMAIN_JOYSTICK, &joystick_prefsChanged); prefs_registerListener(PREF_DOMAIN_JOYSTICK, &joystick_prefsChanged);
video_registerFrameCallback(&joystickReset.frameCallback);
}
static __attribute__((constructor)) void __init_joystick(void) {
emulator_registerStartupCallback(CTOR_PRIORITY_LATE, &_init_joystick);
} }
#ifdef INTERFACE_CLASSIC #ifdef INTERFACE_CLASSIC
@ -270,24 +280,24 @@ void c_calibrate_joystick()
#endif // INTERFACE_CLASSIC #endif // INTERFACE_CLASSIC
#if !TESTING #if !TESTING
// HACK : avoid resetting joystick button values too quickly. This should allow for ClosedApple-Reset. (This is still a // NOTE : reset joystick buttons after a number of frames, which should allow for Open/Closed-Apple reset sequence.
// race, but hopefully much less likely to trigger). void _joystick_frameCallback(uint8_t textFlashCounter) {
static void *_joystick_resetDelayed(void *ctx) { (void)textFlashCounter;
(void)ctx;
SCOPE_TRACE_INTERFACE("_joystick_resetDelayed");
// delay // When activated, this is called every video frame -- ~16.688 millis
sleep(1);
run_args.joy_button0 = 0x0; ASSERT_ON_CPU_THREAD();
run_args.joy_button1 = 0x0;
return NULL; --joystickReset.frameCount;
if (joystickReset.frameCount == 0) {
run_args.joy_button0 = 0x0;
run_args.joy_button1 = 0x0;
joystickReset.frameCallback = NULL; // unlatch
}
} }
#endif #endif
void c_joystick_reset(void) void joystick_reset(void) {
{
if (joydriver_resetJoystick) { if (joydriver_resetJoystick) {
joydriver_resetJoystick(); joydriver_resetJoystick();
} }
@ -297,10 +307,8 @@ void c_joystick_reset(void)
run_args.joy_button0 = 0x0; run_args.joy_button0 = 0x0;
run_args.joy_button1 = 0x0; run_args.joy_button1 = 0x0;
#else #else
pthread_t pid; joystickReset.frameCount = 2; // >= 1 full frame of processing insures that reset is handled
int err = TEMP_FAILURE_RETRY(pthread_create(&pid, NULL, (void *)&_joystick_resetDelayed, (void *)NULL)); joystickReset.frameCallback = &_joystick_frameCallback;
assert(!err);
pthread_detach(pid);
#endif #endif
joy_x = HALF_JOY_RANGE; joy_x = HALF_JOY_RANGE;

View File

@ -43,7 +43,7 @@ extern bool joy_auto_recenter;
extern short joy_step; extern short joy_step;
#endif #endif
void c_joystick_reset(void); void joystick_reset(void);
#ifdef INTERFACE_CLASSIC #ifdef INTERFACE_CLASSIC
void c_calibrate_joystick(void); void c_calibrate_joystick(void);

View File

@ -134,7 +134,7 @@ static void _glnode_initGLUTPost(void) {
glutSpecialUpFunc(gldriver_on_key_special_up); glutSpecialUpFunc(gldriver_on_key_special_up);
//glutMouseFunc(gldriver_mouse); //glutMouseFunc(gldriver_mouse);
//glutMotionFunc(gldriver_mouse_drag); //glutMotionFunc(gldriver_mouse_drag);
c_joystick_reset(); joystick_reset();
} }
#endif #endif

View File

@ -1501,7 +1501,7 @@ void vm_initialize(void) {
_initialize_tables(); _initialize_tables();
disk6_init(); disk6_init();
_initialize_iie_switches(); _initialize_iie_switches();
c_joystick_reset(); joystick_reset();
} }
bool vm_saveState(StateHelper_s *helper) { bool vm_saveState(StateHelper_s *helper) {