From 9ab554af5b6c052c014c4600b48bcfef7f9c0072 Mon Sep 17 00:00:00 2001 From: 3kinox Date: Wed, 14 Oct 2015 13:11:09 +0200 Subject: [PATCH] onepad: + Objectify conf class. + Make some members private as they need to have their range checked before being set + Change "options" variables into an union contraining bitfield representation of it. Allows to make code more expressive/readable. --- plugins/onepad/KeyStatus.cpp | 12 +++--- plugins/onepad/KeyStatus.h | 2 +- plugins/onepad/Linux/dialog.cpp | 23 ++--------- plugins/onepad/Linux/ini.cpp | 12 +++--- plugins/onepad/controller.h | 68 ++++++++++++++++++++++++++++----- plugins/onepad/joystick.cpp | 6 +-- plugins/onepad/keyboard.cpp | 8 ++-- 7 files changed, 82 insertions(+), 49 deletions(-) diff --git a/plugins/onepad/KeyStatus.cpp b/plugins/onepad/KeyStatus.cpp index 98d438a084..11c2f410d1 100644 --- a/plugins/onepad/KeyStatus.cpp +++ b/plugins/onepad/KeyStatus.cpp @@ -69,7 +69,7 @@ void KeyStatus::press(u32 pad, u32 index, s32 value) // Normal mode : expect value 0 -> 80 -> FF // Reverse mode: expect value FF -> 7F -> 0 u8 force = (value / 256); - if (analog_is_reversed(index)) analog_set(pad, index, 0x7F - force); + if (analog_is_reversed(pad,index)) analog_set(pad, index, 0x7F - force); else analog_set(pad, index, 0x80 + force); } } @@ -117,25 +117,25 @@ void KeyStatus::analog_set(u32 pad, u32 index, u8 value) } } -bool KeyStatus::analog_is_reversed(u32 index) +bool KeyStatus::analog_is_reversed(u32 pad, u32 index) { switch (index) { case PAD_L_RIGHT: case PAD_L_LEFT: - return ((conf->options & PADOPTION_REVERSELX) != 0); + return (conf->pad_options[pad].reverse_lx); case PAD_R_LEFT: case PAD_R_RIGHT: - return ((conf->options & PADOPTION_REVERSERX) != 0); + return (conf->pad_options[pad].reverse_rx); case PAD_L_UP: case PAD_L_DOWN: - return ((conf->options & PADOPTION_REVERSELY) != 0); + return (conf->pad_options[pad].reverse_ly); case PAD_R_DOWN: case PAD_R_UP: - return ((conf->options & PADOPTION_REVERSERY) != 0); + return (conf->pad_options[pad].reverse_ry); default: return false; } diff --git a/plugins/onepad/KeyStatus.h b/plugins/onepad/KeyStatus.h index 847f64bc19..98b912f928 100644 --- a/plugins/onepad/KeyStatus.h +++ b/plugins/onepad/KeyStatus.h @@ -49,7 +49,7 @@ class KeyStatus PADAnalog m_internal_analog_joy[2]; void analog_set(u32 pad, u32 index, u8 value); - bool analog_is_reversed(u32 index); + bool analog_is_reversed(u32 pad, u32 index); u8 analog_merge(u8 kbd, u8 joy); public: diff --git a/plugins/onepad/Linux/dialog.cpp b/plugins/onepad/Linux/dialog.cpp index e81caf3244..cbd693e569 100644 --- a/plugins/onepad/Linux/dialog.cpp +++ b/plugins/onepad/Linux/dialog.cpp @@ -294,7 +294,7 @@ typedef struct mask = mask_value; gtk_fixed_put(GTK_FIXED(area), widget, x, y); - gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), mask & conf->options); + gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), mask & conf->packed_options); g_signal_connect(widget, "toggled", G_CALLBACK(on_toggle_option), this); } } dialog_checkbox; @@ -393,9 +393,9 @@ void on_toggle_option(GtkToggleButton *togglebutton, gpointer user_data) dialog_checkbox *checkbox = (dialog_checkbox*)user_data; if (gtk_toggle_button_get_active(togglebutton)) - conf->options |= checkbox->mask; + conf->packed_options |= checkbox->mask; else - conf->options &= ~checkbox->mask; + conf->packed_options &= ~checkbox->mask; } void joy_changed(GtkComboBoxText *box, gpointer user_data) @@ -427,23 +427,6 @@ void pad_changed(GtkNotebook *notebook, void *notebook_page, int page, void *dat set_current_joy(); } -//void on_forcefeedback_toggled(GtkToggleButton *togglebutton, gpointer user_data) -//{ -// int mask = PADOPTION_REVERSELX << (16 * s_selectedpad); -// -// if (gtk_toggle_button_get_active(togglebutton)) -// { -// conf->options |= mask; -// -// u32 joyid = conf->get_joyid(current_pad); -// if (JoystickIdWithinBounds(joyid)) s_vjoysticks[joyid]->TestForce(); -// } -// else -// { -// conf->options &= ~mask; -// } -//} - struct button_positions { const char* label; diff --git a/plugins/onepad/Linux/ini.cpp b/plugins/onepad/Linux/ini.cpp index 0a0933edd9..a88ea433d2 100644 --- a/plugins/onepad/Linux/ini.cpp +++ b/plugins/onepad/Linux/ini.cpp @@ -129,10 +129,10 @@ void SaveConfig() } fprintf(f, "log = %d\n", conf->log); - fprintf(f, "options = %d\n", conf->options); - fprintf(f, "mouse_sensibility = %d\n", conf->sensibility); + fprintf(f, "options = %d\n", conf->packed_options); + fprintf(f, "mouse_sensibility = %d\n", conf->get_sensibility()); fprintf(f, "joy_pad_map = %d\n", conf->joyid_map); - fprintf(f, "ff_intensity = %d\n", conf->ff_intensity); + fprintf(f, "ff_intensity = %d\n", conf->get_ff_intensity()); for (int pad = 0; pad < 2; pad++) { @@ -174,13 +174,13 @@ void LoadConfig() if (fscanf(f, "log = %d\n", &value) == 0) goto error; conf->log = value; if (fscanf(f, "options = %d\n", &value) == 0) goto error; - conf->options = value; + conf->packed_options = value; if (fscanf(f, "mouse_sensibility = %d\n", &value) == 0) goto error; - conf->sensibility = value; + conf->set_sensibility(value); if (fscanf(f, "joy_pad_map = %d\n", &value) == 0) goto error; conf->joyid_map = value; if (fscanf(f, "ff_intensity = %d\n", &value) == 0) goto error; - conf->ff_intensity = value; + conf->set_ff_intensity(value); for (int pad = 0; pad < 2; pad++) { diff --git a/plugins/onepad/controller.h b/plugins/onepad/controller.h index 1f640dfe1e..41db6513e7 100644 --- a/plugins/onepad/controller.h +++ b/plugins/onepad/controller.h @@ -19,8 +19,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ -#ifndef __CONTROLLER_H__ -#define __CONTROLLER_H__ +#pragma once #ifdef __linux__ #define MAX_KEYS 24 @@ -55,23 +54,37 @@ extern int hat_to_key(int dir, int axis_id); extern int PadEnum[2][2]; -struct PADconf +class PADconf { + u32 ff_intensity; + u32 sensibility; public: + union { + struct { + u32 forcefeedback :1; + u32 reverse_lx :1; + u32 reverse_ly :1; + u32 reverse_rx :1; + u32 reverse_ry :1; + u32 mouse_l :1; + u32 mouse_r :1; + u32 sixaxis_usb :1; + u32 _free : 8; // The 8 remaining bits are unused, do what you wish with them ;) + } pad_options[2]; // One for each pads + u32 packed_options; // Only first 8 bits of each 16 bits series are really used, rest is padding + }; + u32 keys[2][MAX_KEYS]; u32 log; - u32 options; // upper 16 bits are for pad2 - u32 sensibility; u32 joyid_map; - u32 ff_intensity; map keysym_map[2]; PADconf() { init(); } void init() { memset(&keys, 0, sizeof(keys)); - log = options = joyid_map = 0; - ff_intensity = 100; + log = packed_options = joyid_map = 0; + ff_intensity = 0x7FFF; // set it at max value by default sensibility = 500; for (int pad = 0; pad < 2 ; pad++) keysym_map[pad].clear(); @@ -87,6 +100,43 @@ struct PADconf int shift = 8 * pad; return ((joyid_map >> shift) & 0xFF); } + + /** + * Return (a copy of) private memner ff_instensity + **/ + u32 get_ff_intensity() + { + return ff_intensity; + } + + /** + * Set intensity while checking that the new value is within + * valid range, more than 0x7FFF will cause pad not to rumble(and less than 0 is obviously bad) + **/ + void set_ff_intensity(u32 new_intensity) + { + if(new_intensity < 0x7FFF && new_intensity >= 0) + { + ff_intensity = new_intensity; + } + } + + /** + * Set sensibility value, sensibility is not yet implemented(and will probably be after evdev) + * However, there will be an upper range too, less than 0 is an obvious wrong + * Anyway, we are doing object oriented code, members are definitely not supposed to be public + **/ + void set_sensibility(u32 new_sensibility) + { + if(sensibility > 0) + { + sensibility = new_sensibility; + } + } + + u32 get_sensibility() + { + return sensibility; + } }; extern PADconf *conf; -#endif diff --git a/plugins/onepad/joystick.cpp b/plugins/onepad/joystick.cpp index 94884fdcbf..3d941f46c6 100644 --- a/plugins/onepad/joystick.cpp +++ b/plugins/onepad/joystick.cpp @@ -146,7 +146,7 @@ void JoystickInfo::InitHapticEffect() void JoystickInfo::DoHapticEffect(int type, int pad, int force) { if (type > 1) return; - if ( !(conf->options & (PADOPTION_FORCEFEEDBACK << 16 * pad)) ) return; + if ( !(conf->pad_options[pad].forcefeedback) ) return; #if SDL_MAJOR_VERSION >= 2 int joyid = conf->get_joyid(pad); @@ -157,7 +157,7 @@ void JoystickInfo::DoHapticEffect(int type, int pad, int force) if (pjoy->haptic_effect_id[type] < 0) return; // FIXME: might need to multiply force - pjoy->haptic_effect_data[type].periodic.magnitude = force * conf->ff_intensity ; // force/32767 strength + pjoy->haptic_effect_data[type].periodic.magnitude = force * conf->get_ff_intensity() ; // force/32767 strength // Upload the new effect SDL_HapticUpdateEffect(pjoy->haptic, pjoy->haptic_effect_id[type], &pjoy->haptic_effect_data[type]); @@ -289,7 +289,7 @@ bool JoystickInfo::PollAxes(u32 &pkey) if (found_hack != string::npos) { // The analog mode of the hat button is quite erratic. Values can be in half- axis // or full axis... So better keep them as button for the moment -- gregory - if (i >= 8 && i <= 11 && (conf->options & PADOPTION_SIXAXIS_USB)) + if (i >= 8 && i <= 11 && (conf->pad_options[pad].sixaxis_usb)) continue; // Disable accelerometer if ((i >= 4 && i <= 6)) diff --git a/plugins/onepad/keyboard.cpp b/plugins/onepad/keyboard.cpp index 2426bbd9f1..f5df504103 100644 --- a/plugins/onepad/keyboard.cpp +++ b/plugins/onepad/keyboard.cpp @@ -142,11 +142,11 @@ void AnalyzeKeyEvent(int pad, keyEvent &evt) // 1/ small move == no move. Cons : can not do small movement // 2/ use a watchdog timer thread // 3/ ??? idea welcome ;) - if (conf->options & ((PADOPTION_MOUSE_L|PADOPTION_MOUSE_R) << 16 * pad )) { + if (conf->pad_options[pad].mouse_l|conf->pad_options[pad].mouse_r) { unsigned int pad_x; unsigned int pad_y; // Note when both PADOPTION_MOUSE_R and PADOPTION_MOUSE_L are set, take only the right one - if (conf->options & (PADOPTION_MOUSE_R << 16 * pad)) { + if (conf->pad_options[pad].mouse_r) { pad_x = PAD_R_RIGHT; pad_y = PAD_R_UP; } else { @@ -156,7 +156,7 @@ void AnalyzeKeyEvent(int pad, keyEvent &evt) unsigned x = evt.key & 0xFFFF; unsigned int value = (s_previous_mouse_x > x) ? s_previous_mouse_x - x : x - s_previous_mouse_x; - value *= conf->sensibility; + value *= conf->get_sensibility(); if (x == 0) key_status->press(pad, pad_x, -MAX_ANALOG_VALUE); @@ -172,7 +172,7 @@ void AnalyzeKeyEvent(int pad, keyEvent &evt) unsigned y = evt.key >> 16; value = (s_previous_mouse_y > y) ? s_previous_mouse_y - y : y - s_previous_mouse_y; - value *= conf->sensibility; + value *= conf->get_sensibility(); if (y == 0) key_status->press(pad, pad_y, -MAX_ANALOG_VALUE);