Move joystick + related functions under JoyDevices

This commit is contained in:
Jean Raby 2022-02-18 13:50:47 -05:00
parent e1685b05eb
commit 38b1e495ea
6 changed files with 131 additions and 113 deletions

View File

@ -207,70 +207,6 @@ int Snes9xConfig::load_defaults()
return 0;
}
void Snes9xConfig::joystick_register_centers()
{
for (auto &j : joysticks)
j.second->register_centers();
}
void Snes9xConfig::flush_joysticks()
{
for (auto &j : joysticks)
j.second->flush();
}
void Snes9xConfig::set_joystick_mode(int mode)
{
for (auto &j : joysticks)
j.second->mode = mode;
}
bool Snes9xConfig::joystick_add(int sdl_device_index)
{
std::array<bool, NUM_JOYPADS> joynums;
for (auto &j : joysticks)
{
joynums[j.second->joynum] = true;
}
// New joystick always gets the lowest available joynum
int joynum(0);
for (; joynum < NUM_JOYPADS && joynums[joynum]; ++joynum);
if (joynum == NUM_JOYPADS)
{
printf("Joystick slots are full, cannot add joystick (device index %d)\n", sdl_device_index);
return false;
}
auto ujd = std::make_unique<JoyDevice>();
ujd->set_sdl_joystick(sdl_device_index, joynum);
printf("Joystick %d, %s", ujd->joynum+1, ujd->description.c_str());
joysticks[ujd->instance_id] = std::move(ujd);
return true;
}
bool Snes9xConfig::joystick_remove(SDL_JoystickID instance_id)
{
if (!joysticks.count(instance_id))
{
printf("joystick_remove: invalid instance id %d", instance_id);
return false;
}
printf("Removed joystick %d, %s", joysticks[instance_id]->joynum+1, joysticks[instance_id]->description.c_str());
joysticks.erase(instance_id);
return true;
}
JoyDevice *Snes9xConfig::joystick_get(SDL_JoystickID instance_id)
{
if (joysticks.count(instance_id)){
return joysticks[instance_id].get();
}
return NULL;
}
int Snes9xConfig::save_config_file()
{
ConfigFile cf;

View File

@ -58,12 +58,6 @@ class Snes9xConfig
int save_config_file();
int load_defaults();
void rebind_keys();
void flush_joysticks();
void set_joystick_mode(int mode);
void joystick_register_centers();
bool joystick_add(int sdl_device_index);
bool joystick_remove(SDL_JoystickID instance_id);
JoyDevice *joystick_get(SDL_JoystickID instance_id);
/* Screen options */
bool full_screen_on_open;
@ -172,7 +166,7 @@ class Snes9xConfig
#endif
std::map<SDL_JoystickID, std::unique_ptr<JoyDevice>> joysticks;
JoyDevices joysticks;
int joystick_threshold;
};

View File

@ -427,38 +427,6 @@ s9xcommand_t S9xGetPortCommandT(const char *name)
}
void JoyDevice::poll_joystick_events()
{
SDL_Event event;
JoyDevice *jd;
while (SDL_PollEvent(&event))
{
switch(event.type) {
case SDL_JOYAXISMOTION:
jd = gui_config->joystick_get(event.jaxis.which);
break;
case SDL_JOYHATMOTION:
jd = gui_config->joystick_get(event.jhat.which);
break;
case SDL_JOYBUTTONUP:
case SDL_JOYBUTTONDOWN:
jd = gui_config->joystick_get(event.jbutton.which);
break;
case SDL_JOYDEVICEADDED:
gui_config->joystick_add(event.jdevice.which);
continue;
case SDL_JOYDEVICEREMOVED:
gui_config->joystick_remove(event.jdevice.which);
continue;
}
if (jd)
{
jd->handle_event(&event);
}
}
}
void S9xProcessEvents(bool8 block)
{
JoyEvent event;
@ -466,7 +434,7 @@ void S9xProcessEvents(bool8 block)
if (S9xGrabJoysticks())
{
JoyDevice::poll_joystick_events();
gui_config->joysticks.poll_events();
for (auto &j : gui_config->joysticks)
{
while (j.second->get_event(&event))
@ -489,7 +457,7 @@ void S9xInitInputDevices()
for (size_t i = 0; i < num_joysticks; i++)
{
gui_config->joystick_add(i);
gui_config->joysticks.add(i);
}
//First plug in both, they'll change later as needed
@ -770,3 +738,105 @@ void JoyDevice::flush()
while (!queue.empty())
queue.pop();
}
void JoyDevices::clear()
{
joysticks.clear();
}
bool JoyDevices::add(int sdl_device_index)
{
std::array<bool, NUM_JOYPADS> joynums;
for (auto &j : joysticks)
{
joynums[j.second->joynum] = true;
}
// New joystick always gets the lowest available joynum
int joynum(0);
for (; joynum < NUM_JOYPADS && joynums[joynum]; ++joynum);
if (joynum == NUM_JOYPADS)
{
printf("Joystick slots are full, cannot add joystick (device index %d)\n", sdl_device_index);
return false;
}
auto ujd = std::make_unique<JoyDevice>();
ujd->set_sdl_joystick(sdl_device_index, joynum);
printf("Joystick %d, %s", ujd->joynum+1, ujd->description.c_str());
joysticks[ujd->instance_id] = std::move(ujd);
return true;
}
bool JoyDevices::remove(SDL_JoystickID instance_id)
{
if (!joysticks.count(instance_id))
{
printf("joystick_remove: invalid instance id %d", instance_id);
return false;
}
printf("Removed joystick %d, %s", joysticks[instance_id]->joynum+1, joysticks[instance_id]->description.c_str());
joysticks.erase(instance_id);
return true;
}
JoyDevice *JoyDevices::get_joystick(SDL_JoystickID instance_id)
{
if (joysticks.count(instance_id)){
return joysticks[instance_id].get();
}
printf("BUG: Event for unknown joystick instance id: %d", instance_id);
return NULL;
}
void JoyDevices::register_centers()
{
for (auto &j : joysticks)
j.second->register_centers();
}
void JoyDevices::flush_events()
{
for (auto &j : joysticks)
j.second->flush();
}
void JoyDevices::set_mode(int mode)
{
for (auto &j : joysticks)
j.second->mode = mode;
}
void JoyDevices::poll_events()
{
SDL_Event event;
JoyDevice *jd;
while (SDL_PollEvent(&event))
{
switch(event.type) {
case SDL_JOYAXISMOTION:
jd = get_joystick(event.jaxis.which);
break;
case SDL_JOYHATMOTION:
jd = get_joystick(event.jhat.which);
break;
case SDL_JOYBUTTONUP:
case SDL_JOYBUTTONDOWN:
jd = get_joystick(event.jbutton.which);
break;
case SDL_JOYDEVICEADDED:
add(event.jdevice.which);
continue;
case SDL_JOYDEVICEREMOVED:
remove(event.jdevice.which);
continue;
}
if (jd)
{
jd->handle_event(&event);
}
}
}

View File

@ -118,6 +118,24 @@ class JoyDevice
void add_event(unsigned int parameter, unsigned int state);
};
class JoyDevices
{
public:
void clear();
bool add(int sdl_device_index);
bool remove(SDL_JoystickID instance_id);
void register_centers();
void flush_events();
void set_mode(int mode);
void poll_events();
std::map<SDL_JoystickID, std::unique_ptr<JoyDevice>>::const_iterator begin() const { return joysticks.begin(); }
std::map<SDL_JoystickID, std::unique_ptr<JoyDevice>>::const_iterator end() const { return joysticks.end(); }
private:
JoyDevice *get_joystick(SDL_JoystickID instance_id);
std::map<SDL_JoystickID, std::unique_ptr<JoyDevice>> joysticks;
};
void S9xDeinitInputDevices();
Binding S9xGetBindingByName(const char *name);

View File

@ -33,11 +33,11 @@ void snes9x_preferences_open(Snes9xWindow *window)
preferences->window->set_transient_for(*window->window.get());
config->set_joystick_mode(JOY_MODE_GLOBAL);
config->joysticks.set_mode(JOY_MODE_GLOBAL);
preferences->show();
window->unpause_from_focus_change();
config->set_joystick_mode(JOY_MODE_INDIVIDUAL);
config->joysticks.set_mode(JOY_MODE_INDIVIDUAL);
config->rebind_keys();
window->update_accelerators();
@ -50,7 +50,7 @@ gboolean poll_joystick(gpointer data)
Binding binding;
int focus;
JoyDevice::poll_joystick_events();
window->config->joysticks.poll_events();
for (auto &j : window->config->joysticks)
{
while (j.second->get_event(&event))
@ -66,7 +66,7 @@ gboolean poll_joystick(gpointer data)
window->store_binding(b_links[focus].button_name,
binding);
window->config->flush_joysticks();
window->config->joysticks.flush_events();
return true;
}
}
@ -949,7 +949,7 @@ void Snes9xPreferences::bindings_to_dialog(int joypad)
void Snes9xPreferences::calibration_dialog()
{
config->joystick_register_centers();
config->joysticks.register_centers();
auto dialog = Gtk::MessageDialog(_("Current joystick centers have been saved."));
dialog.set_title(_("Calibration Complete"));
dialog.run();

View File

@ -149,7 +149,7 @@ int main(int argc, char *argv[])
if (gui_config->fullscreen)
top_level->enter_fullscreen_mode();
gui_config->flush_joysticks();
gui_config->joysticks.flush_events();
if (rom_filename && *Settings.InitialSnapshotFilename)
S9xUnfreezeGame(Settings.InitialSnapshotFilename);
@ -276,7 +276,7 @@ static bool S9xPauseFunc()
if (!Settings.Paused) /* Coming out of pause */
{
/* Clear joystick queues */
gui_config->flush_joysticks();
gui_config->joysticks.flush_events();
S9xSoundStart();
@ -318,7 +318,7 @@ static bool S9xIdleFunc()
{
S9xSoundStop();
gui_config->flush_joysticks();
gui_config->joysticks.flush_events();
if (Settings.NetPlay && NetPlay.Connected)
{