Subversion Repositories eduke32

Rev

Rev 3127 | Rev 3196 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 Plagman 1
/*
2
 * control.c
3
 * MACT library controller handling
809 terminx 4
 *
5 Plagman 5
 * Derived from MACT386.LIB disassembly by Jonathon Fowler
6
 *
7
 */
8
 
434 Plagman 9
#include "compat.h"
1346 terminx 10
 
5 Plagman 11
#include "keyboard.h"
12
#include "mouse.h"
2728 hendricks2 13
#include "joystick.h"
5 Plagman 14
#include "control.h"
15
#include "_control.h"
16
 
17
#include "baselayer.h"
855 terminx 18
#include "osd.h"
5 Plagman 19
#include "pragmas.h"
20
 
21
 
1346 terminx 22
int32_t CONTROL_JoyPresent = FALSE;
23
int32_t CONTROL_JoystickEnabled = FALSE;
24
int32_t CONTROL_MousePresent = FALSE;
25
int32_t CONTROL_MouseEnabled = FALSE;
1492 terminx 26
uint64_t  CONTROL_ButtonState = 0;
27
uint64_t  CONTROL_ButtonHeldState = 0;
5 Plagman 28
 
1346 terminx 29
// static int32_t CONTROL_UserInputDelay = -1;
1355 terminx 30
float  CONTROL_MouseSensitivity = DEFAULTMOUSESENSITIVITY;
1346 terminx 31
static int32_t CONTROL_NumMouseButtons = 0;
32
static int32_t CONTROL_NumMouseAxes = 0;
33
static int32_t CONTROL_NumJoyButtons = 0;
34
static int32_t CONTROL_NumJoyAxes = 0;
5 Plagman 35
static controlflags       CONTROL_Flags[CONTROL_NUM_FLAGS];
36
static controlbuttontype  CONTROL_MouseButtonMapping[MAXMOUSEBUTTONS],
289 terminx 37
CONTROL_JoyButtonMapping[MAXJOYBUTTONS];
3124 helixhorne 38
//static controlkeymaptype  CONTROL_KeyMapping[CONTROL_NUM_FLAGS];
5 Plagman 39
static controlaxismaptype CONTROL_MouseAxesMap[MAXMOUSEAXES],   // maps physical axes onto virtual ones
289 terminx 40
CONTROL_JoyAxesMap[MAXJOYAXES];
5 Plagman 41
static controlaxistype    CONTROL_MouseAxes[MAXMOUSEAXES],      // physical axes
289 terminx 42
CONTROL_JoyAxes[MAXJOYAXES];
5 Plagman 43
static controlaxistype    CONTROL_LastMouseAxes[MAXMOUSEAXES],
289 terminx 44
CONTROL_LastJoyAxes[MAXJOYAXES];
1346 terminx 45
static int32_t   CONTROL_MouseAxesScale[MAXMOUSEAXES],             CONTROL_JoyAxesScale[MAXJOYAXES];
46
static int32_t   CONTROL_MouseButtonState[MAXMOUSEBUTTONS],        CONTROL_JoyButtonState[MAXJOYBUTTONS];
47
static int32_t   CONTROL_MouseButtonClickedTime[MAXMOUSEBUTTONS],  CONTROL_JoyButtonClickedTime[MAXJOYBUTTONS];
48
static int32_t CONTROL_MouseButtonClickedState[MAXMOUSEBUTTONS], CONTROL_JoyButtonClickedState[MAXJOYBUTTONS];
49
static int32_t CONTROL_MouseButtonClicked[MAXMOUSEBUTTONS],      CONTROL_JoyButtonClicked[MAXJOYBUTTONS];
50
static uint8_t CONTROL_MouseButtonClickedCount[MAXMOUSEBUTTONS], CONTROL_JoyButtonClickedCount[MAXJOYBUTTONS];
51
static int32_t CONTROL_UserInputCleared[3];
52
static int32_t(*GetTime)(void);
1552 terminx 53
int32_t CONTROL_Started = FALSE;
2940 helixhorne 54
//static int32_t ticrate;
1346 terminx 55
static int32_t CONTROL_DoubleClickSpeed;
5 Plagman 56
 
3195 terminx 57
int32_t CONTROL_OSDInput[CONTROL_NUM_FLAGS];
58
keybind CONTROL_KeyBinds[MAXBOUNDKEYS];
59
keybind CONTROL_MouseBinds[MAXMOUSEBUTTONS];
60
int32_t CONTROL_BindsEnabled = 0;
61
int32_t CONTROL_SmoothMouse = 0;
5 Plagman 62
 
3124 helixhorne 63
static void CONTROL_GetMouseDelta(void)
5 Plagman 64
{
1346 terminx 65
    int32_t x,y;
5 Plagman 66
 
289 terminx 67
    MOUSE_GetDelta(&x, &y);
5 Plagman 68
 
3195 terminx 69
    if (CONTROL_SmoothMouse)
1099 terminx 70
    {
1346 terminx 71
        static int32_t lastx = 0, lasty = 0;
1099 terminx 72
 
1454 terminx 73
        CONTROL_MouseAxes[0].analog = (int32_t)(((x + lastx) / 2.0f) * 4.0f * CONTROL_MouseSensitivity);
74
        CONTROL_MouseAxes[1].analog = (int32_t)((((y + lasty) / 2.0f) * 4.0f * CONTROL_MouseSensitivity) * 2.0f);
1099 terminx 75
        lastx = x;
76
        lasty = y;
77
        return;
289 terminx 78
    }
5 Plagman 79
 
1454 terminx 80
    CONTROL_MouseAxes[0].analog = (int32_t)(x * 4.0f * CONTROL_MouseSensitivity);
81
    CONTROL_MouseAxes[1].analog = (int32_t)((y * 4.0f * CONTROL_MouseSensitivity) * 2.0f);
5 Plagman 82
}
83
 
3124 helixhorne 84
static void CONTROL_GetJoyDelta(void)
5 Plagman 85
{
1346 terminx 86
    int32_t i;
289 terminx 87
 
88
    for (i=0; i<joynumaxes; i++)
2624 helixhorne 89
        CONTROL_JoyAxes[i].analog = joyaxis[i]; // >> 5;
5 Plagman 90
}
91
 
3124 helixhorne 92
static int32_t CONTROL_StartJoy(int32_t joy)
5 Plagman 93
{
654 terminx 94
    UNREFERENCED_PARAMETER(joy);
289 terminx 95
    return (inputdevices & 4) == 4;
5 Plagman 96
}
97
 
2940 helixhorne 98
static int32_t CONTROL_GetTime(void)
5 Plagman 99
{
1346 terminx 100
    static int32_t t = 0;
289 terminx 101
    t += 5;
102
    return t;
5 Plagman 103
}
104
 
1346 terminx 105
static inline int32_t CONTROL_CheckRange(int32_t which)
5 Plagman 106
{
1346 terminx 107
    if ((uint32_t)which >= (uint32_t)CONTROL_NUM_FLAGS) return TRUE;
289 terminx 108
    //Error("CONTROL_CheckRange: Index %d out of valid range for %d control flags.",
109
    //  which, CONTROL_NUM_FLAGS);
1346 terminx 110
    return FALSE;
5 Plagman 111
}
112
 
3124 helixhorne 113
static void CONTROL_SetFlag(int32_t which, int32_t active)
5 Plagman 114
{
289 terminx 115
    if (CONTROL_CheckRange(which)) return;
5 Plagman 116
 
809 terminx 117
    if (CONTROL_Flags[which].toggle == INSTANT_ONOFF)
118
    {
289 terminx 119
        CONTROL_Flags[which].active = active;
1018 terminx 120
        return;
809 terminx 121
    }
1018 terminx 122
    if (active)
809 terminx 123
    {
1346 terminx 124
        CONTROL_Flags[which].buttonheld = FALSE;
289 terminx 125
    }
1346 terminx 126
    else if (CONTROL_Flags[which].buttonheld == FALSE)
1018 terminx 127
    {
1346 terminx 128
        CONTROL_Flags[which].buttonheld = TRUE;
129
        CONTROL_Flags[which].active = (CONTROL_Flags[which].active ? FALSE : TRUE);
1018 terminx 130
    }
5 Plagman 131
}
132
 
3124 helixhorne 133
#if 0
1346 terminx 134
int32_t CONTROL_KeyboardFunctionPressed(int32_t which)
5 Plagman 135
{
1346 terminx 136
    int32_t key1 = 0, key2 = 0;
5 Plagman 137
 
1346 terminx 138
    if (CONTROL_CheckRange(which)) return FALSE;
5 Plagman 139
 
1346 terminx 140
    if (!CONTROL_Flags[which].used) return FALSE;
5 Plagman 141
 
1206 terminx 142
    if (CONTROL_KeyMapping[which].key1 != KEYUNDEFINED && !KeyBindings[CONTROL_KeyMapping[which].key1].cmd[0])
1346 terminx 143
        key1 = KB_KeyDown[ CONTROL_KeyMapping[which].key1 ] ? TRUE : FALSE;
5 Plagman 144
 
1206 terminx 145
    if (CONTROL_KeyMapping[which].key2 != KEYUNDEFINED && !KeyBindings[CONTROL_KeyMapping[which].key2].cmd[0])
1346 terminx 146
        key2 = KB_KeyDown[ CONTROL_KeyMapping[which].key2 ] ? TRUE : FALSE;
5 Plagman 147
 
289 terminx 148
    return (key1 | key2);
5 Plagman 149
}
150
 
1346 terminx 151
void CONTROL_ClearKeyboardFunction(int32_t which)
5 Plagman 152
{
289 terminx 153
    if (CONTROL_CheckRange(which)) return;
5 Plagman 154
 
289 terminx 155
    if (!CONTROL_Flags[which].used) return;
5 Plagman 156
 
289 terminx 157
    if (CONTROL_KeyMapping[which].key1 != KEYUNDEFINED)
158
        KB_KeyDown[ CONTROL_KeyMapping[which].key1 ] = 0;
5 Plagman 159
 
289 terminx 160
    if (CONTROL_KeyMapping[which].key2 != KEYUNDEFINED)
161
        KB_KeyDown[ CONTROL_KeyMapping[which].key2 ] = 0;
5 Plagman 162
}
3124 helixhorne 163
#endif
5 Plagman 164
 
1346 terminx 165
void CONTROL_DefineFlag(int32_t which, int32_t toggle)
5 Plagman 166
{
289 terminx 167
    if (CONTROL_CheckRange(which)) return;
5 Plagman 168
 
1346 terminx 169
    CONTROL_Flags[which].active     = FALSE;
170
    CONTROL_Flags[which].used       = TRUE;
289 terminx 171
    CONTROL_Flags[which].toggle     = toggle;
1346 terminx 172
    CONTROL_Flags[which].buttonheld = FALSE;
289 terminx 173
    CONTROL_Flags[which].cleared    = 0;
5 Plagman 174
}
175
 
1346 terminx 176
int32_t CONTROL_FlagActive(int32_t which)
5 Plagman 177
{
1346 terminx 178
    if (CONTROL_CheckRange(which)) return FALSE;
5 Plagman 179
 
289 terminx 180
    return CONTROL_Flags[which].used;
5 Plagman 181
}
182
 
3124 helixhorne 183
#if 0
1346 terminx 184
void CONTROL_MapKey(int32_t which, kb_scancode key1, kb_scancode key2)
5 Plagman 185
{
289 terminx 186
    if (CONTROL_CheckRange(which)) return;
5 Plagman 187
 
289 terminx 188
    CONTROL_KeyMapping[which].key1 = key1 ? key1 : KEYUNDEFINED;
189
    CONTROL_KeyMapping[which].key2 = key2 ? key2 : KEYUNDEFINED;
5 Plagman 190
}
191
 
192
void CONTROL_PrintKeyMap(void)
193
{
1346 terminx 194
    int32_t i;
5 Plagman 195
 
1346 terminx 196
    for (i=0; i<CONTROL_NUM_FLAGS; i++)
809 terminx 197
    {
1740 helixhorne 198
        initprintf("function %2d key1=%3x key2=%3x\n",
289 terminx 199
                   i, CONTROL_KeyMapping[i].key1, CONTROL_KeyMapping[i].key2);
200
    }
5 Plagman 201
}
3124 helixhorne 202
#endif
5 Plagman 203
 
1346 terminx 204
void CONTROL_PrintControlFlag(int32_t which)
5 Plagman 205
{
1740 helixhorne 206
    initprintf("function %2d active=%d used=%d toggle=%d buttonheld=%d cleared=%d\n",
289 terminx 207
               which, CONTROL_Flags[which].active, CONTROL_Flags[which].used,
208
               CONTROL_Flags[which].toggle, CONTROL_Flags[which].buttonheld,
209
               CONTROL_Flags[which].cleared);
5 Plagman 210
}
211
 
212
void CONTROL_PrintAxes(void)
213
{
1346 terminx 214
    int32_t i;
5 Plagman 215
 
584 terminx 216
    initprintf("nummouseaxes=%d\n", CONTROL_NumMouseAxes);
1346 terminx 217
    for (i=0; i<CONTROL_NumMouseAxes; i++)
809 terminx 218
    {
584 terminx 219
        initprintf("axis=%d analog=%d digital1=%d digital2=%d\n",
289 terminx 220
                   i, CONTROL_MouseAxesMap[i].analogmap,
221
                   CONTROL_MouseAxesMap[i].minmap, CONTROL_MouseAxesMap[i].maxmap);
222
    }
5 Plagman 223
 
584 terminx 224
    initprintf("numjoyaxes=%d\n", CONTROL_NumJoyAxes);
1346 terminx 225
    for (i=0; i<CONTROL_NumJoyAxes; i++)
809 terminx 226
    {
584 terminx 227
        initprintf("axis=%d analog=%d digital1=%d digital2=%d\n",
289 terminx 228
                   i, CONTROL_JoyAxesMap[i].analogmap,
229
                   CONTROL_JoyAxesMap[i].minmap, CONTROL_JoyAxesMap[i].maxmap);
230
    }
5 Plagman 231
}
232
 
1346 terminx 233
void CONTROL_MapButton(int32_t whichfunction, int32_t whichbutton, int32_t doubleclicked, controldevice device)
5 Plagman 234
{
289 terminx 235
    controlbuttontype *set;
5 Plagman 236
 
289 terminx 237
    if (CONTROL_CheckRange(whichfunction)) whichfunction = BUTTONUNDEFINED;
5 Plagman 238
 
809 terminx 239
    switch (device)
240
    {
289 terminx 241
    case controldevice_mouse:
1346 terminx 242
        if ((uint32_t)whichbutton >= (uint32_t)MAXMOUSEBUTTONS)
809 terminx 243
        {
289 terminx 244
            //Error("CONTROL_MapButton: button %d out of valid range for %d mouse buttons.",
245
            //          whichbutton, CONTROL_NumMouseButtons);
246
            return;
247
        }
248
        set = CONTROL_MouseButtonMapping;
249
        break;
5 Plagman 250
 
289 terminx 251
    case controldevice_joystick:
1346 terminx 252
        if ((uint32_t)whichbutton >= (uint32_t)MAXJOYBUTTONS)
809 terminx 253
        {
289 terminx 254
            //Error("CONTROL_MapButton: button %d out of valid range for %d joystick buttons.",
255
            //          whichbutton, CONTROL_NumJoyButtons);
256
            return;
257
        }
258
        set = CONTROL_JoyButtonMapping;
259
        break;
260
 
261
    default:
262
        //Error("CONTROL_MapButton: invalid controller device type");
263
        return;
264
    }
265
 
266
    if (doubleclicked)
267
        set[whichbutton].doubleclicked = whichfunction;
268
    else
269
        set[whichbutton].singleclicked = whichfunction;
5 Plagman 270
}
271
 
1346 terminx 272
void CONTROL_MapAnalogAxis(int32_t whichaxis, int32_t whichanalog, controldevice device)
5 Plagman 273
{
289 terminx 274
    controlaxismaptype *set;
5 Plagman 275
 
1346 terminx 276
    if ((uint32_t)whichanalog >= (uint32_t)analog_maxtype)
809 terminx 277
    {
289 terminx 278
        //Error("CONTROL_MapAnalogAxis: analog function %d out of valid range for %d analog functions.",
279
        //              whichanalog, analog_maxtype);
280
        return;
281
    }
5 Plagman 282
 
809 terminx 283
    switch (device)
284
    {
289 terminx 285
    case controldevice_mouse:
1346 terminx 286
        if ((uint32_t)whichaxis >= (uint32_t)MAXMOUSEAXES)
809 terminx 287
        {
289 terminx 288
            //Error("CONTROL_MapAnalogAxis: axis %d out of valid range for %d mouse axes.",
289
            //          whichaxis, MAXMOUSEAXES);
290
            return;
291
        }
5 Plagman 292
 
289 terminx 293
        set = CONTROL_MouseAxesMap;
294
        break;
5 Plagman 295
 
289 terminx 296
    case controldevice_joystick:
1346 terminx 297
        if ((uint32_t)whichaxis >= (uint32_t)MAXJOYAXES)
809 terminx 298
        {
289 terminx 299
            //Error("CONTROL_MapAnalogAxis: axis %d out of valid range for %d joystick axes.",
300
            //          whichaxis, MAXJOYAXES);
301
            return;
302
        }
5 Plagman 303
 
289 terminx 304
        set = CONTROL_JoyAxesMap;
305
        break;
5 Plagman 306
 
289 terminx 307
    default:
308
        //Error("CONTROL_MapAnalogAxis: invalid controller device type");
309
        return;
310
    }
5 Plagman 311
 
289 terminx 312
    set[whichaxis].analogmap = whichanalog;
5 Plagman 313
}
314
 
1346 terminx 315
void CONTROL_SetAnalogAxisScale(int32_t whichaxis, int32_t axisscale, controldevice device)
5 Plagman 316
{
1346 terminx 317
    int32_t *set;
5 Plagman 318
 
809 terminx 319
    switch (device)
320
    {
289 terminx 321
    case controldevice_mouse:
1346 terminx 322
        if ((uint32_t)whichaxis >= (uint32_t)MAXMOUSEAXES)
809 terminx 323
        {
289 terminx 324
            //Error("CONTROL_SetAnalogAxisScale: axis %d out of valid range for %d mouse axes.",
325
            //          whichaxis, MAXMOUSEAXES);
326
            return;
327
        }
5 Plagman 328
 
289 terminx 329
        set = CONTROL_MouseAxesScale;
330
        break;
5 Plagman 331
 
289 terminx 332
    case controldevice_joystick:
1346 terminx 333
        if ((uint32_t)whichaxis >= (uint32_t)MAXJOYAXES)
809 terminx 334
        {
289 terminx 335
            //Error("CONTROL_SetAnalogAxisScale: axis %d out of valid range for %d joystick axes.",
336
            //          whichaxis, MAXJOYAXES);
337
            return;
338
        }
5 Plagman 339
 
289 terminx 340
        set = CONTROL_JoyAxesScale;
341
        break;
5 Plagman 342
 
289 terminx 343
    default:
344
        //Error("CONTROL_SetAnalogAxisScale: invalid controller device type");
345
        return;
346
    }
347
 
348
    set[whichaxis] = axisscale;
5 Plagman 349
}
350
 
1346 terminx 351
void CONTROL_MapDigitalAxis(int32_t whichaxis, int32_t whichfunction, int32_t direction, controldevice device)
5 Plagman 352
{
289 terminx 353
    controlaxismaptype *set;
5 Plagman 354
 
289 terminx 355
    if (CONTROL_CheckRange(whichfunction)) whichfunction = AXISUNDEFINED;
5 Plagman 356
 
809 terminx 357
    switch (device)
358
    {
289 terminx 359
    case controldevice_mouse:
1346 terminx 360
        if ((uint32_t)whichaxis >= (uint32_t)MAXMOUSEAXES)
809 terminx 361
        {
289 terminx 362
            //Error("CONTROL_MapDigitalAxis: axis %d out of valid range for %d mouse axes.",
363
            //          whichaxis, MAXMOUSEAXES);
364
            return;
365
        }
5 Plagman 366
 
289 terminx 367
        set = CONTROL_MouseAxesMap;
368
        break;
5 Plagman 369
 
289 terminx 370
    case controldevice_joystick:
1346 terminx 371
        if ((uint32_t)whichaxis >= (uint32_t)MAXJOYAXES)
809 terminx 372
        {
289 terminx 373
            //Error("CONTROL_MapDigitalAxis: axis %d out of valid range for %d joystick axes.",
374
            //          whichaxis, MAXJOYAXES);
375
            return;
376
        }
5 Plagman 377
 
289 terminx 378
        set = CONTROL_JoyAxesMap;
379
        break;
5 Plagman 380
 
289 terminx 381
    default:
382
        //Error("CONTROL_MapDigitalAxis: invalid controller device type");
383
        return;
384
    }
385
 
809 terminx 386
    switch (direction)          // JBF: this is all very much a guess. The ASM puzzles me.
387
    {
289 terminx 388
    case axis_up:
389
    case axis_left:
390
        set[whichaxis].minmap = whichfunction;
391
        break;
392
    case axis_down:
393
    case axis_right:
394
        set[whichaxis].maxmap = whichfunction;
395
        break;
396
    default:
397
        break;
398
    }
5 Plagman 399
}
400
 
809 terminx 401
void CONTROL_ClearAssignments(void)
5 Plagman 402
{
1346 terminx 403
    int32_t i;
5 Plagman 404
 
289 terminx 405
    memset(CONTROL_MouseButtonMapping,  BUTTONUNDEFINED, sizeof(CONTROL_MouseButtonMapping));
406
    memset(CONTROL_JoyButtonMapping,    BUTTONUNDEFINED, sizeof(CONTROL_JoyButtonMapping));
3124 helixhorne 407
//    memset(CONTROL_KeyMapping,          KEYUNDEFINED,    sizeof(CONTROL_KeyMapping));
289 terminx 408
    memset(CONTROL_MouseAxesMap,        AXISUNDEFINED,   sizeof(CONTROL_MouseAxesMap));
409
    memset(CONTROL_JoyAxesMap,          AXISUNDEFINED,   sizeof(CONTROL_JoyAxesMap));
410
    memset(CONTROL_MouseAxes,           0,               sizeof(CONTROL_MouseAxes));
411
    memset(CONTROL_JoyAxes,             0,               sizeof(CONTROL_JoyAxes));
412
    memset(CONTROL_LastMouseAxes,       0,               sizeof(CONTROL_LastMouseAxes));
413
    memset(CONTROL_LastJoyAxes,         0,               sizeof(CONTROL_LastJoyAxes));
1346 terminx 414
    for (i=0; i<MAXMOUSEAXES; i++)
289 terminx 415
        CONTROL_MouseAxesScale[i] = NORMALAXISSCALE;
1346 terminx 416
    for (i=0; i<MAXJOYAXES; i++)
289 terminx 417
        CONTROL_JoyAxesScale[i] = NORMALAXISSCALE;
5 Plagman 418
}
419
 
420
static void DoGetDeviceButtons(
1346 terminx 421
    int32_t buttons, int32_t tm,
422
    int32_t NumButtons,
423
    int32_t *DeviceButtonState,
424
    int32_t *ButtonClickedTime,
425
    int32_t *ButtonClickedState,
426
    int32_t *ButtonClicked,
427
    uint8_t *ButtonClickedCount
809 terminx 428
)
429
{
2940 helixhorne 430
    int32_t i=NumButtons-1;
5 Plagman 431
 
1346 terminx 432
    for (; i>=0; i--)
809 terminx 433
    {
2940 helixhorne 434
        int32_t bs = (buttons >> i) & 1;
5 Plagman 435
 
289 terminx 436
        DeviceButtonState[i] = bs;
1346 terminx 437
        ButtonClickedState[i] = FALSE;
5 Plagman 438
 
809 terminx 439
        if (bs)
440
        {
1346 terminx 441
            if (ButtonClicked[i] == FALSE)
809 terminx 442
            {
1346 terminx 443
                ButtonClicked[i] = TRUE;
5 Plagman 444
 
809 terminx 445
                if (ButtonClickedCount[i] == 0 || tm > ButtonClickedTime[i])
446
                {
289 terminx 447
                    ButtonClickedTime[i] = tm + CONTROL_DoubleClickSpeed;
448
                    ButtonClickedCount[i] = 1;
449
                }
809 terminx 450
                else if (tm < ButtonClickedTime[i])
451
                {
1346 terminx 452
                    ButtonClickedState[i] = TRUE;
289 terminx 453
                    ButtonClickedTime[i]  = 0;
454
                    ButtonClickedCount[i] = 2;
455
                }
456
            }
809 terminx 457
            else if (ButtonClickedCount[i] == 2)
458
            {
1346 terminx 459
                ButtonClickedState[i] = TRUE;
289 terminx 460
            }
2940 helixhorne 461
 
1015 terminx 462
            continue;
809 terminx 463
        }
2940 helixhorne 464
 
1015 terminx 465
        if (ButtonClickedCount[i] == 2)
466
            ButtonClickedCount[i] = 0;
5 Plagman 467
 
1346 terminx 468
        ButtonClicked[i] = FALSE;
289 terminx 469
    }
5 Plagman 470
}
471
 
3124 helixhorne 472
static void CONTROL_GetDeviceButtons(void)
5 Plagman 473
{
2940 helixhorne 474
    int32_t t = GetTime();
5 Plagman 475
 
809 terminx 476
    if (CONTROL_MouseEnabled)
477
    {
289 terminx 478
        DoGetDeviceButtons(
479
            MOUSE_GetButtons(), t,
480
            CONTROL_NumMouseButtons,
481
            CONTROL_MouseButtonState,
482
            CONTROL_MouseButtonClickedTime,
483
            CONTROL_MouseButtonClickedState,
484
            CONTROL_MouseButtonClicked,
485
            CONTROL_MouseButtonClickedCount
486
        );
487
    }
5 Plagman 488
 
809 terminx 489
    if (CONTROL_JoystickEnabled)
490
    {
2728 hendricks2 491
        int32_t buttons = JOYSTICK_GetButtons();
492
        if (joynumhats > 0)
809 terminx 493
        {
2728 hendricks2 494
            int32_t hat = JOYSTICK_GetHat(0);
495
            if (hat != 0)
496
                buttons |= hat << min(MAXJOYBUTTONS,joynumbuttons);
289 terminx 497
        }
5 Plagman 498
 
289 terminx 499
        DoGetDeviceButtons(
500
            buttons, t,
501
            CONTROL_NumJoyButtons,
502
            CONTROL_JoyButtonState,
503
            CONTROL_JoyButtonClickedTime,
504
            CONTROL_JoyButtonClickedState,
505
            CONTROL_JoyButtonClicked,
506
            CONTROL_JoyButtonClickedCount
507
        );
508
    }
5 Plagman 509
}
510
 
3124 helixhorne 511
static void CONTROL_DigitizeAxis(int32_t axis, controldevice device)
5 Plagman 512
{
289 terminx 513
    controlaxistype *set, *lastset;
5 Plagman 514
 
809 terminx 515
    switch (device)
516
    {
289 terminx 517
    case controldevice_mouse:
518
        set = CONTROL_MouseAxes;
519
        lastset = CONTROL_LastMouseAxes;
520
        break;
5 Plagman 521
 
289 terminx 522
    case controldevice_joystick:
523
        set = CONTROL_JoyAxes;
524
        lastset = CONTROL_LastJoyAxes;
525
        break;
526
 
527
    default: return;
528
    }
529
 
809 terminx 530
    if (set[axis].analog > 0)
531
    {
1018 terminx 532
        if (set[axis].analog > THRESHOLD || (set[axis].analog > MINTHRESHOLD && lastset[axis].digital == 1))
533
            set[axis].digital = 1;
809 terminx 534
    }
535
    else
536
    {
1018 terminx 537
        if (set[axis].analog < -THRESHOLD || (set[axis].analog < -MINTHRESHOLD && lastset[axis].digital == -1))
289 terminx 538
            set[axis].digital = -1;
539
    }
5 Plagman 540
}
541
 
3124 helixhorne 542
static void CONTROL_ScaleAxis(int32_t axis, controldevice device)
5 Plagman 543
{
289 terminx 544
    controlaxistype *set;
1346 terminx 545
    int32_t *scale;
5 Plagman 546
 
809 terminx 547
    switch (device)
548
    {
289 terminx 549
    case controldevice_mouse:
550
        set = CONTROL_MouseAxes;
551
        scale = CONTROL_MouseAxesScale;
552
        break;
5 Plagman 553
 
289 terminx 554
    case controldevice_joystick:
555
        set = CONTROL_JoyAxes;
556
        scale = CONTROL_JoyAxesScale;
557
        break;
5 Plagman 558
 
289 terminx 559
    default: return;
560
    }
561
 
562
    set[axis].analog = mulscale16(set[axis].analog, scale[axis]);
5 Plagman 563
}
564
 
3124 helixhorne 565
static void CONTROL_ApplyAxis(int32_t axis, ControlInfo *info, controldevice device)
5 Plagman 566
{
289 terminx 567
    controlaxistype *set;
568
    controlaxismaptype *map;
5 Plagman 569
 
809 terminx 570
    switch (device)
571
    {
289 terminx 572
    case controldevice_mouse:
573
        set = CONTROL_MouseAxes;
574
        map = CONTROL_MouseAxesMap;
575
        break;
5 Plagman 576
 
289 terminx 577
    case controldevice_joystick:
578
        set = CONTROL_JoyAxes;
579
        map = CONTROL_JoyAxesMap;
580
        break;
5 Plagman 581
 
289 terminx 582
    default: return;
583
    }
584
 
809 terminx 585
    switch (map[axis].analogmap)
586
    {
289 terminx 587
    case analog_turning:          info->dyaw   += set[axis].analog; break;
588
    case analog_strafing:         info->dx     += set[axis].analog; break;
589
    case analog_lookingupanddown: info->dpitch += set[axis].analog; break;
590
    case analog_elevation:        info->dy     += set[axis].analog; break;
591
    case analog_rolling:          info->droll  += set[axis].analog; break;
592
    case analog_moving:           info->dz     += set[axis].analog; break;
593
    default: break;
594
    }
5 Plagman 595
}
596
 
2940 helixhorne 597
static void CONTROL_PollDevices(ControlInfo *info)
5 Plagman 598
{
1425 terminx 599
    Bmemcpy(CONTROL_LastMouseAxes, CONTROL_MouseAxes, sizeof(CONTROL_MouseAxes));
600
    Bmemcpy(CONTROL_LastJoyAxes,   CONTROL_JoyAxes,   sizeof(CONTROL_JoyAxes));
5 Plagman 601
 
289 terminx 602
    memset(CONTROL_MouseAxes, 0, sizeof(CONTROL_MouseAxes));
603
    memset(CONTROL_JoyAxes,   0, sizeof(CONTROL_JoyAxes));
604
    memset(info, 0, sizeof(ControlInfo));
5 Plagman 605
 
809 terminx 606
    if (CONTROL_MouseEnabled)
607
    {
1346 terminx 608
        int32_t i = MAXMOUSEAXES-1;
1018 terminx 609
 
289 terminx 610
        CONTROL_GetMouseDelta();
2334 helixhorne 611
        for (; i>=0; i--)
809 terminx 612
        {
289 terminx 613
            CONTROL_DigitizeAxis(i, controldevice_mouse);
614
            CONTROL_ScaleAxis(i, controldevice_mouse);
615
            LIMITCONTROL(&CONTROL_MouseAxes[i].analog);
616
            CONTROL_ApplyAxis(i, info, controldevice_mouse);
1017 terminx 617
        }
289 terminx 618
    }
1014 terminx 619
 
809 terminx 620
    if (CONTROL_JoystickEnabled)
621
    {
1346 terminx 622
        int32_t i = MAXJOYAXES-1;
1018 terminx 623
 
289 terminx 624
        CONTROL_GetJoyDelta();
2334 helixhorne 625
        for (; i>=0; i--)
809 terminx 626
        {
289 terminx 627
            CONTROL_DigitizeAxis(i, controldevice_joystick);
628
            CONTROL_ScaleAxis(i, controldevice_joystick);
629
            LIMITCONTROL(&CONTROL_JoyAxes[i].analog);
630
            CONTROL_ApplyAxis(i, info, controldevice_joystick);
1017 terminx 631
        }
289 terminx 632
    }
633
 
634
    CONTROL_GetDeviceButtons();
5 Plagman 635
}
636
 
3124 helixhorne 637
static void CONTROL_AxisFunctionState(int32_t *p1)
5 Plagman 638
{
1016 terminx 639
    if (CONTROL_NumMouseAxes)
809 terminx 640
    {
1346 terminx 641
        int32_t j, i = CONTROL_NumMouseAxes-1;
5 Plagman 642
 
1016 terminx 643
        do
644
        {
645
            if (!CONTROL_MouseAxes[i].digital) continue;
5 Plagman 646
 
1016 terminx 647
            if (CONTROL_MouseAxes[i].digital < 0)
648
                j = CONTROL_MouseAxesMap[i].minmap;
649
            else
650
                j = CONTROL_MouseAxesMap[i].maxmap;
5 Plagman 651
 
1016 terminx 652
            if (j != AXISUNDEFINED)
653
                p1[j] = 1;
1017 terminx 654
        }
1023 terminx 655
        while (i--);
1016 terminx 656
    }
1014 terminx 657
 
1016 terminx 658
    if (CONTROL_NumJoyAxes)
809 terminx 659
    {
1346 terminx 660
        int32_t j, i = CONTROL_NumJoyAxes-1;
5 Plagman 661
 
1016 terminx 662
        do
663
        {
664
            if (!CONTROL_JoyAxes[i].digital) continue;
289 terminx 665
 
1016 terminx 666
            if (CONTROL_JoyAxes[i].digital < 0)
667
                j = CONTROL_JoyAxesMap[i].minmap;
668
            else
669
                j = CONTROL_JoyAxesMap[i].maxmap;
670
 
671
            if (j != AXISUNDEFINED)
672
                p1[j] = 1;
1017 terminx 673
        }
1023 terminx 674
        while (i--);
1016 terminx 675
    }
5 Plagman 676
}
677
 
3124 helixhorne 678
static void CONTROL_ButtonFunctionState(int32_t *p1)
5 Plagman 679
{
1018 terminx 680
    if (CONTROL_NumMouseButtons)
681
    {
1346 terminx 682
        int32_t i = CONTROL_NumMouseButtons-1, j;
5 Plagman 683
 
1018 terminx 684
        do
826 terminx 685
        {
3195 terminx 686
            if (!CONTROL_MouseBinds[i].cmd[0])
1018 terminx 687
            {
688
                j = CONTROL_MouseButtonMapping[i].doubleclicked;
689
                if (j != KEYUNDEFINED)
690
                    p1[j] |= CONTROL_MouseButtonClickedState[i];
826 terminx 691
 
1018 terminx 692
                j = CONTROL_MouseButtonMapping[i].singleclicked;
693
                if (j != KEYUNDEFINED)
694
                    p1[j] |= CONTROL_MouseButtonState[i];
695
            }
5 Plagman 696
 
3195 terminx 697
            if (!CONTROL_BindsEnabled)
1018 terminx 698
                continue;
1017 terminx 699
 
3195 terminx 700
            if (CONTROL_MouseBinds[i].cmd[0] && CONTROL_MouseButtonState[i])
1018 terminx 701
            {
3195 terminx 702
                if (CONTROL_MouseBinds[i].repeat || (CONTROL_MouseBinds[i].laststate == 0))
703
                    OSD_Dispatch(CONTROL_MouseBinds[i].cmd);
1018 terminx 704
            }
3195 terminx 705
            CONTROL_MouseBinds[i].laststate = CONTROL_MouseButtonState[i];
1017 terminx 706
        }
1023 terminx 707
        while (i--);
1017 terminx 708
    }
709
 
1018 terminx 710
    if (CONTROL_NumJoyButtons)
809 terminx 711
    {
1346 terminx 712
        int32_t i=CONTROL_NumJoyButtons-1, j;
5 Plagman 713
 
1018 terminx 714
        do
715
        {
716
            j = CONTROL_JoyButtonMapping[i].doubleclicked;
717
            if (j != KEYUNDEFINED)
718
                p1[j] |= CONTROL_JoyButtonClickedState[i];
719
 
720
            j = CONTROL_JoyButtonMapping[i].singleclicked;
721
            if (j != KEYUNDEFINED)
722
                p1[j] |= CONTROL_JoyButtonState[i];
723
        }
1023 terminx 724
        while (i--);
1017 terminx 725
    }
5 Plagman 726
}
727
 
1346 terminx 728
void CONTROL_ClearButton(int32_t whichbutton)
5 Plagman 729
{
809 terminx 730
    if (CONTROL_CheckRange(whichbutton)) return;
731
    BUTTONCLEAR(whichbutton);
1346 terminx 732
    CONTROL_Flags[whichbutton].cleared = TRUE;
5 Plagman 733
}
734
 
1632 terminx 735
void CONTROL_ProcessBinds(void)
808 terminx 736
{
1632 terminx 737
    int32_t i=MAXBOUNDKEYS-1;
738
 
3195 terminx 739
    if (!CONTROL_BindsEnabled)
1425 terminx 740
        return;
808 terminx 741
 
1632 terminx 742
    do
808 terminx 743
    {
3195 terminx 744
        if (CONTROL_KeyBinds[i].cmd[0])
808 terminx 745
        {
3195 terminx 746
            if (KB_KeyPressed(i) && (CONTROL_KeyBinds[i].repeat || (CONTROL_KeyBinds[i].laststate == 0)))
1018 terminx 747
            {
3195 terminx 748
                OSD_Dispatch(CONTROL_KeyBinds[i].cmd);
1018 terminx 749
            }
3127 helixhorne 750
 
3195 terminx 751
            CONTROL_KeyBinds[i].laststate = KB_KeyPressed(i);
808 terminx 752
        }
1017 terminx 753
    }
1632 terminx 754
    while (i--);
808 terminx 755
}
756
 
2940 helixhorne 757
static void CONTROL_GetFunctionInput(void)
5 Plagman 758
{
1346 terminx 759
    int32_t periphs[CONTROL_NUM_FLAGS];
1632 terminx 760
    int32_t i = CONTROL_NUM_FLAGS-1;
5 Plagman 761
 
289 terminx 762
    memset(periphs, 0, sizeof(periphs));
763
    CONTROL_ButtonFunctionState(periphs);
764
    CONTROL_AxisFunctionState(periphs);
5 Plagman 765
 
1144 terminx 766
    CONTROL_ButtonHeldState = CONTROL_ButtonState;
767
    CONTROL_ButtonState = 0;
5 Plagman 768
 
1632 terminx 769
    do
809 terminx 770
    {
3195 terminx 771
        CONTROL_SetFlag(i, /*CONTROL_KeyboardFunctionPressed(i) | */periphs[i] | CONTROL_OSDInput[i]);
5 Plagman 772
 
1632 terminx 773
        if (CONTROL_Flags[i].cleared == FALSE) BUTTONSET(i, CONTROL_Flags[i].active);
774
        else if (CONTROL_Flags[i].active == FALSE) CONTROL_Flags[i].cleared = 0;
289 terminx 775
    }
1632 terminx 776
    while (i--);
1014 terminx 777
 
3195 terminx 778
    memset(CONTROL_OSDInput, 0, sizeof(CONTROL_OSDInput));
5 Plagman 779
}
780
 
2728 hendricks2 781
void CONTROL_GetInput(ControlInfo *info)
782
{
783
    CONTROL_PollDevices(info);
784
 
785
    CONTROL_GetFunctionInput();
786
}
787
 
1346 terminx 788
int32_t CONTROL_Startup(controltype which, int32_t(*TimeFunction)(void), int32_t ticspersecond)
5 Plagman 789
{
1346 terminx 790
    int32_t i;
5 Plagman 791
 
654 terminx 792
    UNREFERENCED_PARAMETER(which);
649 terminx 793
 
1346 terminx 794
    if (CONTROL_Started) return FALSE;
5 Plagman 795
 
289 terminx 796
    if (TimeFunction) GetTime = TimeFunction;
797
    else GetTime = CONTROL_GetTime;
5 Plagman 798
 
2940 helixhorne 799
//    ticrate = ticspersecond;
5 Plagman 800
 
289 terminx 801
    CONTROL_DoubleClickSpeed = (ticspersecond*57)/100;
802
    if (CONTROL_DoubleClickSpeed <= 0)
803
        CONTROL_DoubleClickSpeed = 1;
5 Plagman 804
 
1346 terminx 805
    if (initinput()) return TRUE;
5 Plagman 806
 
1346 terminx 807
    CONTROL_MousePresent = CONTROL_MouseEnabled    = FALSE;
808
    CONTROL_JoyPresent   = CONTROL_JoystickEnabled = FALSE;
289 terminx 809
    CONTROL_NumMouseButtons = CONTROL_NumJoyButtons = 0;
810
    CONTROL_NumMouseAxes    = CONTROL_NumJoyAxes    = 0;
811
    KB_Startup();
5 Plagman 812
 
289 terminx 813
    //switch (which) {
814
    //  case controltype_keyboard:
815
    //          break;
5 Plagman 816
 
289 terminx 817
    //  case controltype_keyboardandmouse:
818
    CONTROL_NumMouseAxes      = MAXMOUSEAXES;
819
    CONTROL_NumMouseButtons   = MAXMOUSEBUTTONS;
2624 helixhorne 820
    CONTROL_MousePresent      = Mouse_Init();
289 terminx 821
    CONTROL_MouseEnabled      = CONTROL_MousePresent;
822
    //          break;
5 Plagman 823
 
289 terminx 824
    //  case controltype_keyboardandjoystick:
825
    CONTROL_NumJoyAxes    = min(MAXJOYAXES,joynumaxes);
826
    CONTROL_NumJoyButtons = min(MAXJOYBUTTONS,joynumbuttons + 4*(joynumhats>0));
2334 helixhorne 827
    CONTROL_JoyPresent    = CONTROL_StartJoy(0);
289 terminx 828
    CONTROL_JoystickEnabled = CONTROL_JoyPresent;
829
    //          break;
830
    //}
5 Plagman 831
 
2624 helixhorne 832
#ifdef GEKKO
289 terminx 833
    if (CONTROL_MousePresent)
834
        initprintf("CONTROL_Startup: Mouse Present\n");
835
    if (CONTROL_JoyPresent)
836
        initprintf("CONTROL_Startup: Joystick Present\n");
2334 helixhorne 837
#endif
5 Plagman 838
 
1144 terminx 839
    CONTROL_ButtonState     = 0;
840
    CONTROL_ButtonHeldState = 0;
289 terminx 841
 
842
    memset(CONTROL_UserInputCleared, 0, sizeof(CONTROL_UserInputCleared));
843
 
844
    for (i=0; i<CONTROL_NUM_FLAGS; i++)
1346 terminx 845
        CONTROL_Flags[i].used = FALSE;
289 terminx 846
 
1346 terminx 847
    CONTROL_Started = TRUE;
289 terminx 848
 
1346 terminx 849
    return FALSE;
5 Plagman 850
}
851
 
852
void CONTROL_Shutdown(void)
853
{
289 terminx 854
    if (!CONTROL_Started) return;
5 Plagman 855
 
1346 terminx 856
    CONTROL_JoyPresent = FALSE;
5 Plagman 857
 
289 terminx 858
    MOUSE_Shutdown();
859
    uninitinput();
5 Plagman 860
 
1346 terminx 861
    CONTROL_Started = FALSE;
5 Plagman 862
}
863