diff --git a/sc2/src/libs/inplib.h b/sc2/src/libs/inplib.h index 418ace3fd..e7b238773 100644 --- a/sc2/src/libs/inplib.h +++ b/sc2/src/libs/inplib.h @@ -23,13 +23,10 @@ #include "libs/compiler.h" #include "libs/uio.h" -typedef DWORD INPUT_STATE; -extern BOOLEAN InitInput (void); -extern BOOLEAN UninitInput (void); extern BOOLEAN AnyButtonPress (BOOLEAN DetectSpecial); -extern void FlushInput (void); +extern void TFB_ResetControls (void); /* * Not used right now diff --git a/sc2/src/libs/input/inpintrn.h b/sc2/src/libs/input/inpintrn.h index 78859302d..064f654c5 100644 --- a/sc2/src/libs/input/inpintrn.h +++ b/sc2/src/libs/input/inpintrn.h @@ -20,6 +20,6 @@ #define _INPINTRN_H #include "libs/inplib.h" - +#include "libs/input/input_common.h" #endif /* _INPINTRN_H */ diff --git a/sc2/src/libs/input/input_common.c b/sc2/src/libs/input/input_common.c index dc48e0cd1..7a0bbc107 100644 --- a/sc2/src/libs/input/input_common.c +++ b/sc2/src/libs/input/input_common.c @@ -17,7 +17,4 @@ */ #include "port.h" -#include "input_common.h" #include "inpintrn.h" - -volatile BOOLEAN ExitRequested, GamePaused; diff --git a/sc2/src/libs/input/input_common.h b/sc2/src/libs/input/input_common.h index 6ec6a0ca8..5979320fb 100644 --- a/sc2/src/libs/input/input_common.h +++ b/sc2/src/libs/input/input_common.h @@ -28,7 +28,12 @@ enum // flags for TFB_InitInput //#define TFB_INPUTFLAGS_ETC (1<<0) -int TFB_InitInput (int driver, int flags); -void TFB_UninitInput (void); +extern int TFB_InitInput (int driver, int flags); +extern void TFB_UninitInput (void); + +#define MAX_FLIGHT_ALTERNATES 2 + +extern void TFB_SetInputVectors (volatile int menu[], int num_menu, + volatile int flight[], int num_templ, int num_flight); #endif diff --git a/sc2/src/libs/input/sdl/input.c b/sc2/src/libs/input/sdl/input.c index 294405d05..926a395b9 100644 --- a/sc2/src/libs/input/sdl/input.c +++ b/sc2/src/libs/input/sdl/input.c @@ -20,8 +20,8 @@ #include #include +#include "../inpintrn.h" #include "libs/graphics/sdl/sdl_common.h" -#include "libs/input/input_common.h" #include "libs/input/sdl/vcontrol.h" #include "libs/input/sdl/keynames.h" #include "libs/memlib.h" @@ -29,8 +29,6 @@ #include "libs/log.h" #include "libs/reslib.h" #include "options.h" -// XXX: we should not include anything from uqm/ inside libs/ -#include "uqm/controls.h" #define KBDBUFSIZE (1 << 8) @@ -41,6 +39,14 @@ static int num_keys = 0; static int *kbdstate = NULL; // Holds all SDL keys +1 for holding invalid values +static volatile int *menu_vec; +static int num_menu; +// The last vector element is the character repeat "key" +#define KEY_MENU_ANY (num_menu - 1) +static volatile int *flight_vec; +static int num_templ; +static int num_flight; + static BOOLEAN InputInitialized = FALSE; static BOOLEAN in_character_mode = FALSE; @@ -98,12 +104,15 @@ register_menu_controls (int index) if (!res_HasKey (buf)) break; VControl_ParseGesture (&g, res_GetString (buf)); - VControl_AddGestureBinding (&g, (int *)&ImmediateInputState.menu[index]); + VControl_AddGestureBinding (&g, (int *)&menu_vec[index]); i++; } } -static VCONTROL_GESTURE controls[NUM_TEMPLATES][NUM_KEYS][2]; + +static VCONTROL_GESTURE *controls; +#define CONTROL_PTR(i, j, k) \ + (controls + ((i) * num_flight + (j)) * MAX_FLIGHT_ALTERNATES + (k)) static void register_flight_controls (void) @@ -113,7 +122,7 @@ register_flight_controls (void) buf[39] = '\0'; - for (i = 0; i < NUM_TEMPLATES; i++) + for (i = 0; i < num_templ; i++) { /* Copy in name */ snprintf (buf, 39, "keys.%d.name", i+1); @@ -126,11 +135,11 @@ register_flight_controls (void) { input_templates[i].name[0] = '\0'; } - for (j = 0; j < NUM_KEYS; j++) + for (j = 0; j < num_flight; j++) { - for (k = 0; k < 2; k++) + for (k = 0; k < MAX_FLIGHT_ALTERNATES; k++) { - VCONTROL_GESTURE *g = &controls[i][j][k]; + VCONTROL_GESTURE *g = CONTROL_PTR(i, j, k); snprintf (buf, 39, "keys.%d.%s.%d", i+1, flight_res_names[j], k+1); if (!res_HasKey (buf)) { @@ -138,7 +147,7 @@ register_flight_controls (void) continue; } VControl_ParseGesture (g, res_GetString (buf)); - VControl_AddGestureBinding (g, (int *)&ImmediateInputState.key[i][j]); + VControl_AddGestureBinding (g, (int *)(flight_vec + i * num_flight + j)); } } } @@ -149,10 +158,19 @@ initKeyConfig (void) { int i; + if (!menu_vec || !flight_vec) + { + log_add (log_Fatal, "initKeyConfig(): invalid input vectors"); + exit (EXIT_FAILURE); + } + + controls = HCalloc (sizeof (*controls) * num_templ * num_flight + * MAX_FLIGHT_ALTERNATES); + /* First, load in the menu keys */ LoadResourceIndex (contentDir, "menu.key", "menu."); LoadResourceIndex (configDir, "override.cfg", "menu."); - for (i = 0; i < NUM_MENU_KEYS; i++) + for (i = 0; i < num_menu; i++) { if (!menu_res_names[i]) break; @@ -178,7 +196,23 @@ static void resetKeyboardState (void) { memset (kbdstate, 0, sizeof (int) * num_keys); - ImmediateInputState.menu[KEY_MENU_ANY] = 0; + menu_vec[KEY_MENU_ANY] = 0; +} + +void +TFB_SetInputVectors (volatile int menu[], int num_menu_, volatile int flight[], + int num_templ_, int num_flight_) +{ + if (num_menu_ < 0 || num_templ_ < 0 || num_flight_ < 0) + { + log_add (log_Fatal, "TFB_SetInputVectors(): invalid vector size"); + exit (EXIT_FAILURE); + } + menu_vec = menu; + num_menu = num_menu_; + flight_vec = flight; + num_templ = num_templ_; + num_flight = num_flight_; } int @@ -189,8 +223,6 @@ TFB_InitInput (int driver, int flags) (void)driver; (void)flags; - GamePaused = ExitRequested = FALSE; - SDL_EnableUNICODE(1); (void)SDL_GetKeyState (&num_keys); kbdstate = (int *)HMalloc (sizeof (int) * (num_keys + 1)); @@ -237,6 +269,7 @@ void TFB_UninitInput (void) { VControl_Uninit (); + HFree (controls); HFree (kbdstate); } @@ -342,7 +375,7 @@ ProcessInputEvent (const SDL_Event *Event) kbdbuf[kbdtail] = map_key; kbdtail = newtail; lastchar = map_key; - ImmediateInputState.menu[KEY_MENU_ANY]++; + menu_vec[KEY_MENU_ANY]++; } } else if (Event->type == SDL_KEYUP) @@ -350,13 +383,13 @@ ProcessInputEvent (const SDL_Event *Event) if (kbdstate[k] == 0) { // something is fishy -- better to reset the // repeatable state to avoid big problems - ImmediateInputState.menu[KEY_MENU_ANY] = 0; + menu_vec[KEY_MENU_ANY] = 0; } else { kbdstate[k]--; - if (ImmediateInputState.menu[KEY_MENU_ANY] > 0) - ImmediateInputState.menu[KEY_MENU_ANY]--; + if (menu_vec[KEY_MENU_ANY] > 0) + menu_vec[KEY_MENU_ANY]--; } } } @@ -372,17 +405,18 @@ TFB_ResetControls (void) lastchar = 0; } -void -FlushInput (void) -{ - TFB_ResetControls (); - FlushInputState (); -} - void InterrogateInputState (int template, int control, int index, char *buffer, int maxlen) { - VCONTROL_GESTURE *g = &controls[template][control][index]; + VCONTROL_GESTURE *g = CONTROL_PTR(template, control, index); + + if (template >= num_templ || control >= num_flight + || index >= MAX_FLIGHT_ALTERNATES) + { + log_add (log_Warning, "InterrogateInputState(): invalid control index"); + buffer[0] = 0; + return; + } switch (g->type) { @@ -411,11 +445,19 @@ InterrogateInputState (int template, int control, int index, char *buffer, int m void RemoveInputState (int template, int control, int index) { - VCONTROL_GESTURE *g = &controls[template][control][index]; + VCONTROL_GESTURE *g = CONTROL_PTR(template, control, index); char keybuf[40]; keybuf[39] = '\0'; - VControl_RemoveGestureBinding (g, (int *)&ImmediateInputState.key[template][control]); + if (template >= num_templ || control >= num_flight + || index >= MAX_FLIGHT_ALTERNATES) + { + log_add (log_Warning, "RemoveInputState(): invalid control index"); + return; + } + + VControl_RemoveGestureBinding (g, + (int *)(flight_vec + template * num_flight + control)); g->type = VCONTROL_NONE; snprintf (keybuf, 39, "keys.%d.%s.%d", template+1, flight_res_names[control], index+1); @@ -431,6 +473,13 @@ RebindInputState (int template, int control, int index) char keybuf[40], valbuf[40]; keybuf[39] = valbuf[39] = '\0'; + if (template >= num_templ || control >= num_flight + || index >= MAX_FLIGHT_ALTERNATES) + { + log_add (log_Warning, "RebindInputState(): invalid control index"); + return; + } + /* Remove the old binding on this spot */ RemoveInputState (template, control, index); @@ -442,8 +491,9 @@ RebindInputState (int template, int control, int index) } /* And now, add the new binding. */ - VControl_AddGestureBinding (&g, (int *)&ImmediateInputState.key[template][control]); - controls[template][control][index] = g; + VControl_AddGestureBinding (&g, + (int *)(flight_vec + template * num_flight + control)); + *CONTROL_PTR(template, control, index) = g; snprintf (keybuf, 39, "keys.%d.%s.%d", template+1, flight_res_names[control], index+1); VControl_DumpGesture (valbuf, 39, &g); res_PutString (keybuf, valbuf); diff --git a/sc2/src/libs/input/sdl/input.h b/sc2/src/libs/input/sdl/input.h index 4688d92f1..c2b8a2017 100644 --- a/sc2/src/libs/input/sdl/input.h +++ b/sc2/src/libs/input/sdl/input.h @@ -19,7 +19,6 @@ #ifndef INPUT_H #define INPUT_H -void FlushInput (void); -void ProcessInputEvent (const SDL_Event *Event); +extern void ProcessInputEvent (const SDL_Event *Event); #endif diff --git a/sc2/src/uqm.c b/sc2/src/uqm.c index 2712d9023..7c616892c 100644 --- a/sc2/src/uqm.c +++ b/sc2/src/uqm.c @@ -482,6 +482,11 @@ main (int argc, char *argv[]) initAudio calls AssignTask, which currently blocks on ProcessThreadLifecycles... */ // initAudio (snddriver, soundflags); + // Make sure that the compiler treats multidim arrays the way we expect + assert (sizeof (int [NUM_TEMPLATES * NUM_KEYS]) == + sizeof (int [NUM_TEMPLATES][NUM_KEYS])); + TFB_SetInputVectors (ImmediateInputState.menu, NUM_MENU_KEYS, + (volatile int *)ImmediateInputState.key, NUM_TEMPLATES, NUM_KEYS); TFB_InitInput (TFB_INPUTDRIVER_SDL, 0); StartThread (Starcon2Main, NULL, 1024, "Starcon2Main"); diff --git a/sc2/src/uqm/comm/starbas/starbas.c b/sc2/src/uqm/comm/starbas/starbas.c index e9c30dbe8..1e7938a6a 100644 --- a/sc2/src/uqm/comm/starbas/starbas.c +++ b/sc2/src/uqm/comm/starbas/starbas.c @@ -24,7 +24,6 @@ #include "uqm/setup.h" #include "uqm/shipcont.h" #include "libs/graphics/gfx_common.h" -#include "libs/inplib.h" #include "libs/mathlib.h" #include "libs/inplib.h" #include "libs/sound/sound.h" diff --git a/sc2/src/uqm/confirm.c b/sc2/src/uqm/confirm.c index 5aa7650a6..4f74f2940 100644 --- a/sc2/src/uqm/confirm.c +++ b/sc2/src/uqm/confirm.c @@ -25,7 +25,6 @@ #include "sounds.h" #include "gamestr.h" #include "libs/graphics/widgets.h" -#include "libs/inplib.h" #include "libs/sound/trackplayer.h" #include "libs/log.h" #include "libs/resource/stringbank.h" @@ -266,7 +265,6 @@ DoPopupWindow (const char *msg) DrawStamp (&s); DestroyDrawable (ReleaseDrawable (s.frame)); - FlushInput (); SetContextClipRect (&oldRect); SetContext (oldContext); UnlockMutex (GraphicsLock); diff --git a/sc2/src/uqm/controls.h b/sc2/src/uqm/controls.h index 207243b6a..27d9a011e 100644 --- a/sc2/src/uqm/controls.h +++ b/sc2/src/uqm/controls.h @@ -95,8 +95,7 @@ extern volatile CONTROLLER_INPUT_STATE ImmediateInputState; extern CONTROL_TEMPLATE PlayerControls[]; void UpdateInputState (void); -void FlushInputState (void); -void TFB_ResetControls (void); +extern void FlushInput (void); void SetMenuRepeatDelay (DWORD min, DWORD max, DWORD step, BOOLEAN gestalt); void SetDefaultMenuRepeatDelay (void); void ResetKeyRepeat (void); diff --git a/sc2/src/uqm/cyborg.c b/sc2/src/uqm/cyborg.c index 2204f282c..f2e6c329d 100644 --- a/sc2/src/uqm/cyborg.c +++ b/sc2/src/uqm/cyborg.c @@ -24,7 +24,6 @@ #include "intel.h" #include "setup.h" #include "units.h" -#include "libs/inplib.h" #include "libs/mathlib.h" #include "libs/log.h" @@ -572,7 +571,7 @@ ThrustShip (ELEMENT *ShipPtr, COUNT angle) void Pursue (ELEMENT *ShipPtr, EVALUATE_DESC *EvalDescPtr) { - INPUT_STATE maneuver_state; + BYTE maneuver_state; COUNT desired_thrust_angle, desired_turn_angle; SIZE delta_x, delta_y; SIZE ship_delta_x, ship_delta_y; @@ -703,7 +702,7 @@ Pursue (ELEMENT *ShipPtr, EVALUATE_DESC *EvalDescPtr) void Entice (ELEMENT *ShipPtr, EVALUATE_DESC *EvalDescPtr) { - INPUT_STATE maneuver_state; + BYTE maneuver_state; COUNT desired_thrust_angle, desired_turn_angle; COUNT cone_of_fire, travel_angle; SIZE delta_x, delta_y; diff --git a/sc2/src/uqm/gameinp.c b/sc2/src/uqm/gameinp.c index 78c6bb4a6..16795fe9f 100644 --- a/sc2/src/uqm/gameinp.c +++ b/sc2/src/uqm/gameinp.c @@ -65,6 +65,9 @@ static MENU_SOUND_FLAGS sound_0, sound_1; volatile CONTROLLER_INPUT_STATE ImmediateInputState; +volatile BOOLEAN ExitRequested; +volatile BOOLEAN GamePaused; + static void _clear_menu_state (void) { @@ -291,8 +294,9 @@ SetDefaultMenuRepeatDelay () } void -FlushInputState (void) +FlushInput (void) { + TFB_ResetControls (); _clear_menu_state (); } @@ -334,7 +338,7 @@ DoInput (void *pInputState, BOOLEAN resetInput) SetMenuRepeatDelay (ACCELERATION_INCREMENT, MENU_REPEAT_DELAY, ACCELERATION_INCREMENT, FALSE); if (resetInput) - TFB_ResetControls (); + FlushInput (); do { @@ -376,7 +380,7 @@ DoInput (void *pInputState, BOOLEAN resetInput) } while (((INPUT_STATE_DESC*)pInputState)->InputFunc (pInputState)); if (resetInput) - TFB_ResetControls (); + FlushInput (); } void diff --git a/sc2/src/uqm/gameopt.c b/sc2/src/uqm/gameopt.c index 130070794..9b6d643c0 100644 --- a/sc2/src/uqm/gameopt.c +++ b/sc2/src/uqm/gameopt.c @@ -25,7 +25,6 @@ #include "encount.h" #include "planets/lander.h" #include "gamestr.h" -#include "libs/inplib.h" #include "load.h" #include "options.h" #include "save.h" diff --git a/sc2/src/uqm/intro.c b/sc2/src/uqm/intro.c index b6ff35f62..c0606bf1f 100644 --- a/sc2/src/uqm/intro.c +++ b/sc2/src/uqm/intro.c @@ -28,7 +28,6 @@ #include "libs/graphics/drawable.h" #include "libs/sound/sound.h" #include "libs/vidlib.h" -#include "libs/inplib.h" #include "libs/log.h" #include @@ -785,7 +784,6 @@ ShowSlidePresentation (STRING PresStr) OldFont = SetContextFont (NULL); UnlockMutex (GraphicsLock); - FlushInput (); SetMenuSounds (MENU_SOUND_NONE, MENU_SOUND_NONE); pis.MenuRepeatDelay = 0; pis.InputFunc = DoPresentation; @@ -891,8 +889,6 @@ ShowLegacyVideo (LEGACY_VIDEO vid) if (!ref) return FALSE; - // XXX: FlushInput() won't be need once DoInput(reset-input) works right - FlushInput (); vis.MenuRepeatDelay = 0; vis.InputFunc = DoVideoInput; vis.CurVideo = ref; diff --git a/sc2/src/uqm/melee.c b/sc2/src/uqm/melee.c index 4d5bcf788..f6220d747 100644 --- a/sc2/src/uqm/melee.c +++ b/sc2/src/uqm/melee.c @@ -48,7 +48,6 @@ #include "util.h" #include "libs/graphics/drawable.h" #include "libs/gfxlib.h" -#include "libs/inplib.h" #include "libs/mathlib.h" #include "libs/log.h" @@ -1098,7 +1097,7 @@ DoEdit (MELEE_STATE *pMS) pMS->Initialized = 0; else pMS->Initialized = -1; - TFB_ResetControls (); + FlushInput (); DoPickShip (pMS); } else if (pMS->row < NUM_MELEE_ROWS && diff --git a/sc2/src/uqm/planets/devices.c b/sc2/src/uqm/planets/devices.c index 448f37b52..c6e47c740 100644 --- a/sc2/src/uqm/planets/devices.c +++ b/sc2/src/uqm/planets/devices.c @@ -651,7 +651,7 @@ Devices (MENU_STATE *pMS) pMS->first_item.y = 0; pMS->CurFrame = (FRAME)DeviceMap; - TFB_ResetControls (); + FlushInput (); DoManipulateDevices (pMS); /* to make sure it's initialized */ SetMenuSounds (MENU_SOUND_ARROWS, MENU_SOUND_SELECT); DoInput (pMS, TRUE); diff --git a/sc2/src/uqm/planets/solarsys.c b/sc2/src/uqm/planets/solarsys.c index 6741c581a..26dbdd2e3 100644 --- a/sc2/src/uqm/planets/solarsys.c +++ b/sc2/src/uqm/planets/solarsys.c @@ -35,7 +35,6 @@ #include "../uqmdebug.h" #include "libs/graphics/gfx_common.h" #include "libs/mathlib.h" -#include "libs/inplib.h" #include "libs/log.h" diff --git a/sc2/src/uqm/restart.c b/sc2/src/uqm/restart.c index 17b6497df..6189bee10 100644 --- a/sc2/src/uqm/restart.c +++ b/sc2/src/uqm/restart.c @@ -317,7 +317,6 @@ RestartMenu (MENU_STATE *pMS) if (TimeOut == ONE_SECOND / 8) SleepThread (ONE_SECOND * 3); DrawRestartMenuGraphic (pMS); - FlushInput (); GLOBAL (CurrentActivity) &= ~CHECK_ABORT; SetMenuSounds (MENU_SOUND_UP | MENU_SOUND_DOWN, MENU_SOUND_SELECT); DoInput (pMS, TRUE); diff --git a/sc2/src/uqm/save.c b/sc2/src/uqm/save.c index 8c9408906..11d028c54 100644 --- a/sc2/src/uqm/save.c +++ b/sc2/src/uqm/save.c @@ -606,7 +606,8 @@ SaveProblem (void) FlushGraphics (); UnlockMutex (GraphicsLock); - while (AnyButtonPress (FALSE)); + while (AnyButtonPress (FALSE)) + ; do { TaskSwitch (); @@ -622,8 +623,6 @@ SaveProblem (void) SetContext (OldContext); DestroyDrawable (ReleaseDrawable (s.frame)); UnlockMutex (GraphicsLock); - - return; } // This function first writes to a memory file, and then writes the whole diff --git a/sc2/src/uqm/starbase.c b/sc2/src/uqm/starbase.c index e5bd63bef..0c38b59f2 100644 --- a/sc2/src/uqm/starbase.c +++ b/sc2/src/uqm/starbase.c @@ -30,7 +30,6 @@ #include "sounds.h" #include "libs/graphics/gfx_common.h" #include "libs/tasklib.h" -#include "libs/inplib.h" MENU_STATE *pMenuState; diff --git a/sc2/src/uqm/starcon.c b/sc2/src/uqm/starcon.c index a87f45154..89a8abe1a 100644 --- a/sc2/src/uqm/starcon.c +++ b/sc2/src/uqm/starcon.c @@ -37,7 +37,6 @@ #include "libs/log.h" #include "libs/gfxlib.h" #include "libs/graphics/gfx_common.h" -#include "libs/inplib.h" #include "libs/misc.h" #include "uqmversion.h"