Subversion Repositories eduke32

Rev

Rev 8483 | Rev 8536 | 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
 
7214 terminx 9
#include "_control.h"
10
#include "baselayer.h"
434 Plagman 11
#include "compat.h"
7214 terminx 12
#include "control.h"
13
#include "joystick.h"
5 Plagman 14
#include "keyboard.h"
15
#include "mouse.h"
855 terminx 16
#include "osd.h"
5 Plagman 17
#include "pragmas.h"
18
 
4386 terminx 19
#ifdef __ANDROID__
20
#include "android.h"
21
#endif
22
 
7181 terminx 23
bool CONTROL_Started         = false;
7214 terminx 24
bool CONTROL_MouseEnabled    = false;
25
bool CONTROL_MousePresent    = false;
7181 terminx 26
bool CONTROL_JoyPresent      = false;
27
bool CONTROL_JoystickEnabled = false;
28
 
6334 terminx 29
uint64_t CONTROL_ButtonState     = 0;
30
uint64_t CONTROL_ButtonHeldState = 0;
5 Plagman 31
 
7992 hendricks2 32
LastSeenInput CONTROL_LastSeenInput;
33
 
6334 terminx 34
float          CONTROL_MouseSensitivity = DEFAULTMOUSESENSITIVITY;
35
static int32_t CONTROL_NumMouseButtons  = 0;
36
static int32_t CONTROL_NumJoyButtons    = 0;
37
static int32_t CONTROL_NumJoyAxes       = 0;
38
 
39
static controlflags      CONTROL_Flags[CONTROL_NUM_FLAGS];
40
 
8303 hendricks2 41
static controlkeymaptype  CONTROL_KeyMapping[CONTROL_NUM_FLAGS];
7214 terminx 42
 
8127 terminx 43
static int32_t            CONTROL_MouseAxesScale[2];
7214 terminx 44
 
6334 terminx 45
static controlaxismaptype CONTROL_JoyAxesMap[MAXJOYAXES];
7214 terminx 46
static controlaxistype    CONTROL_JoyAxes[MAXJOYAXES];
47
static controlaxistype    CONTROL_LastJoyAxes[MAXJOYAXES];
48
static int32_t            CONTROL_JoyAxesScale[MAXJOYAXES];
7986 hendricks2 49
static int8_t             CONTROL_JoyAxesInvert[MAXJOYAXES];
6334 terminx 50
 
7214 terminx 51
static controlbuttontype CONTROL_MouseButtonMapping[MAXMOUSEBUTTONS];
6334 terminx 52
 
7214 terminx 53
static int32_t CONTROL_MouseButtonClicked[MAXMOUSEBUTTONS];
54
static int32_t CONTROL_MouseButtonClickedState[MAXMOUSEBUTTONS];
55
static int32_t CONTROL_MouseButtonClickedTime[MAXMOUSEBUTTONS];
6334 terminx 56
static int32_t CONTROL_MouseButtonState[MAXMOUSEBUTTONS];
57
static uint8_t CONTROL_MouseButtonClickedCount[MAXMOUSEBUTTONS];
58
 
7214 terminx 59
static controlbuttontype CONTROL_JoyButtonMapping[MAXJOYBUTTONS];
60
 
61
static int32_t CONTROL_JoyButtonClicked[MAXJOYBUTTONS];
62
static int32_t CONTROL_JoyButtonClickedState[MAXJOYBUTTONS];
63
static int32_t CONTROL_JoyButtonClickedTime[MAXJOYBUTTONS];
6334 terminx 64
static int32_t CONTROL_JoyButtonState[MAXJOYBUTTONS];
65
static uint8_t CONTROL_JoyButtonClickedCount[MAXJOYBUTTONS];
66
 
4558 hendricks2 67
static int32_t(*ExtGetTime)(void);
2940 helixhorne 68
//static int32_t ticrate;
7181 terminx 69
static uint8_t CONTROL_DoubleClickSpeed;
5 Plagman 70
 
7181 terminx 71
int32_t CONTROL_ButtonFlags[CONTROL_NUM_FLAGS];
72
consolekeybind_t CONTROL_KeyBinds[MAXBOUNDKEYS + MAXMOUSEBUTTONS];
73
bool CONTROL_BindsEnabled = 0;
74
bool CONTROL_SmoothMouse  = 0;
5 Plagman 75
 
6334 terminx 76
#define CONTROL_CheckRange(which) ((unsigned)which >= (unsigned)CONTROL_NUM_FLAGS)
7705 terminx 77
#define BIND(x, s, r, k) do { Xfree(x.cmdstr); x.cmdstr = s; x.repeat = r; x.key = k; } while (0)
3214 terminx 78
 
3209 helixhorne 79
void CONTROL_ClearAllBinds(void)
80
{
7181 terminx 81
    for (int i=0; i<MAXBOUNDKEYS; i++)
3209 helixhorne 82
        CONTROL_FreeKeyBind(i);
7181 terminx 83
    for (int i=0; i<MAXMOUSEBUTTONS; i++)
3209 helixhorne 84
        CONTROL_FreeMouseBind(i);
85
}
86
 
6334 terminx 87
void CONTROL_BindKey(int i, char const * const cmd, int repeat, char const * const keyname)
3209 helixhorne 88
{
7079 terminx 89
    BIND(CONTROL_KeyBinds[i], Xstrdup(cmd), repeat, keyname);
3209 helixhorne 90
}
91
 
6334 terminx 92
void CONTROL_BindMouse(int i, char const * const cmd, int repeat, char const * const keyname)
3209 helixhorne 93
{
7079 terminx 94
    BIND(CONTROL_KeyBinds[MAXBOUNDKEYS + i], Xstrdup(cmd), repeat, keyname);
3209 helixhorne 95
}
96
 
6334 terminx 97
void CONTROL_FreeKeyBind(int i)
3209 helixhorne 98
{
3214 terminx 99
    BIND(CONTROL_KeyBinds[i], NULL, 0, NULL);
3209 helixhorne 100
}
101
 
6334 terminx 102
void CONTROL_FreeMouseBind(int i)
3209 helixhorne 103
{
6328 terminx 104
    BIND(CONTROL_KeyBinds[MAXBOUNDKEYS + i], NULL, 0, NULL);
3209 helixhorne 105
}
106
 
7947 hendricks2 107
static void CONTROL_GetMouseDelta(ControlInfo * info)
5 Plagman 108
{
7142 terminx 109
    vec2_t input;
110
    mouseReadPos(&input.x, &input.y);
5 Plagman 111
 
7142 terminx 112
    vec2f_t finput = { float(input.x), float(input.y) };
5 Plagman 113
 
3195 terminx 114
    if (CONTROL_SmoothMouse)
1099 terminx 115
    {
6773 terminx 116
        static vec2_t last;
7142 terminx 117
        finput = { float(input.x + last.x) * 0.5f, float(input.y + last.y) * 0.5f };
118
        last = input;
289 terminx 119
    }
5 Plagman 120
 
8126 terminx 121
    info->mousex = mulscale16(Blrintf(finput.x * 4.f * CONTROL_MouseSensitivity), CONTROL_MouseAxesScale[0]);
122
    info->mousey = mulscale16(Blrintf(finput.y * 4.f * CONTROL_MouseSensitivity), CONTROL_MouseAxesScale[1]);
5 Plagman 123
}
124
 
2940 helixhorne 125
static int32_t CONTROL_GetTime(void)
5 Plagman 126
{
1346 terminx 127
    static int32_t t = 0;
289 terminx 128
    t += 5;
129
    return t;
5 Plagman 130
}
131
 
7181 terminx 132
static void CONTROL_SetFlag(int which, int active)
5 Plagman 133
{
289 terminx 134
    if (CONTROL_CheckRange(which)) return;
5 Plagman 135
 
6334 terminx 136
    controlflags &flags = CONTROL_Flags[which];
137
 
138
    if (flags.toggle == INSTANT_ONOFF)
139
        flags.active = active;
140
    else if (active)
141
        flags.buttonheld = FALSE;
142
    else if (flags.buttonheld == FALSE)
809 terminx 143
    {
6334 terminx 144
        flags.buttonheld = TRUE;
145
        flags.active = (flags.active ? FALSE : TRUE);
809 terminx 146
    }
5 Plagman 147
}
148
 
1346 terminx 149
int32_t CONTROL_KeyboardFunctionPressed(int32_t which)
5 Plagman 150
{
1346 terminx 151
    int32_t key1 = 0, key2 = 0;
5 Plagman 152
 
1346 terminx 153
    if (CONTROL_CheckRange(which)) return FALSE;
5 Plagman 154
 
1346 terminx 155
    if (!CONTROL_Flags[which].used) return FALSE;
5 Plagman 156
 
8303 hendricks2 157
    if (CONTROL_KeyMapping[which].key1 != KEYUNDEFINED)
1346 terminx 158
        key1 = KB_KeyDown[ CONTROL_KeyMapping[which].key1 ] ? TRUE : FALSE;
5 Plagman 159
 
8303 hendricks2 160
    if (CONTROL_KeyMapping[which].key2 != KEYUNDEFINED)
1346 terminx 161
        key2 = KB_KeyDown[ CONTROL_KeyMapping[which].key2 ] ? TRUE : FALSE;
5 Plagman 162
 
5803 terminx 163
    return key1 | key2;
5 Plagman 164
}
165
 
1346 terminx 166
void CONTROL_ClearKeyboardFunction(int32_t which)
5 Plagman 167
{
289 terminx 168
    if (CONTROL_CheckRange(which)) return;
5 Plagman 169
 
289 terminx 170
    if (!CONTROL_Flags[which].used) return;
5 Plagman 171
 
289 terminx 172
    if (CONTROL_KeyMapping[which].key1 != KEYUNDEFINED)
173
        KB_KeyDown[ CONTROL_KeyMapping[which].key1 ] = 0;
5 Plagman 174
 
289 terminx 175
    if (CONTROL_KeyMapping[which].key2 != KEYUNDEFINED)
176
        KB_KeyDown[ CONTROL_KeyMapping[which].key2 ] = 0;
5 Plagman 177
}
178
 
6334 terminx 179
void CONTROL_DefineFlag(int which, int toggle)
5 Plagman 180
{
289 terminx 181
    if (CONTROL_CheckRange(which)) return;
5 Plagman 182
 
6334 terminx 183
    controlflags &flags = CONTROL_Flags[which];
184
 
185
    flags.active     = FALSE;
186
    flags.buttonheld = FALSE;
187
    flags.cleared    = 0;
7181 terminx 188
    flags.toggle     = toggle;
189
    flags.used       = TRUE;
5 Plagman 190
}
191
 
6334 terminx 192
int CONTROL_FlagActive(int which)
5 Plagman 193
{
1346 terminx 194
    if (CONTROL_CheckRange(which)) return FALSE;
5 Plagman 195
 
289 terminx 196
    return CONTROL_Flags[which].used;
5 Plagman 197
}
198
 
1346 terminx 199
void CONTROL_MapKey(int32_t which, kb_scancode key1, kb_scancode key2)
5 Plagman 200
{
289 terminx 201
    if (CONTROL_CheckRange(which)) return;
5 Plagman 202
 
289 terminx 203
    CONTROL_KeyMapping[which].key1 = key1 ? key1 : KEYUNDEFINED;
204
    CONTROL_KeyMapping[which].key2 = key2 ? key2 : KEYUNDEFINED;
5 Plagman 205
}
206
 
8303 hendricks2 207
#if 0
5 Plagman 208
void CONTROL_PrintKeyMap(void)
209
{
1346 terminx 210
    int32_t i;
5 Plagman 211
 
1346 terminx 212
    for (i=0; i<CONTROL_NUM_FLAGS; i++)
809 terminx 213
    {
1740 helixhorne 214
        initprintf("function %2d key1=%3x key2=%3x\n",
289 terminx 215
                   i, CONTROL_KeyMapping[i].key1, CONTROL_KeyMapping[i].key2);
216
    }
5 Plagman 217
}
218
 
1346 terminx 219
void CONTROL_PrintControlFlag(int32_t which)
5 Plagman 220
{
1740 helixhorne 221
    initprintf("function %2d active=%d used=%d toggle=%d buttonheld=%d cleared=%d\n",
289 terminx 222
               which, CONTROL_Flags[which].active, CONTROL_Flags[which].used,
223
               CONTROL_Flags[which].toggle, CONTROL_Flags[which].buttonheld,
224
               CONTROL_Flags[which].cleared);
5 Plagman 225
}
226
 
227
void CONTROL_PrintAxes(void)
228
{
1346 terminx 229
    int32_t i;
5 Plagman 230
 
584 terminx 231
    initprintf("numjoyaxes=%d\n", CONTROL_NumJoyAxes);
1346 terminx 232
    for (i=0; i<CONTROL_NumJoyAxes; i++)
809 terminx 233
    {
584 terminx 234
        initprintf("axis=%d analog=%d digital1=%d digital2=%d\n",
289 terminx 235
                   i, CONTROL_JoyAxesMap[i].analogmap,
236
                   CONTROL_JoyAxesMap[i].minmap, CONTROL_JoyAxesMap[i].maxmap);
237
    }
5 Plagman 238
}
3209 helixhorne 239
#endif
5 Plagman 240
 
6334 terminx 241
void CONTROL_MapButton(int whichfunction, int whichbutton, int doubleclicked, controldevice device)
5 Plagman 242
{
289 terminx 243
    controlbuttontype *set;
5 Plagman 244
 
289 terminx 245
    if (CONTROL_CheckRange(whichfunction)) whichfunction = BUTTONUNDEFINED;
5 Plagman 246
 
809 terminx 247
    switch (device)
248
    {
289 terminx 249
    case controldevice_mouse:
6334 terminx 250
        if ((unsigned)whichbutton >= (unsigned)MAXMOUSEBUTTONS)
809 terminx 251
        {
289 terminx 252
            //Error("CONTROL_MapButton: button %d out of valid range for %d mouse buttons.",
253
            //          whichbutton, CONTROL_NumMouseButtons);
254
            return;
255
        }
256
        set = CONTROL_MouseButtonMapping;
257
        break;
5 Plagman 258
 
289 terminx 259
    case controldevice_joystick:
6334 terminx 260
        if ((unsigned)whichbutton >= (unsigned)MAXJOYBUTTONS)
809 terminx 261
        {
289 terminx 262
            //Error("CONTROL_MapButton: button %d out of valid range for %d joystick buttons.",
263
            //          whichbutton, CONTROL_NumJoyButtons);
264
            return;
265
        }
266
        set = CONTROL_JoyButtonMapping;
267
        break;
268
 
269
    default:
270
        //Error("CONTROL_MapButton: invalid controller device type");
271
        return;
272
    }
273
 
274
    if (doubleclicked)
275
        set[whichbutton].doubleclicked = whichfunction;
276
    else
277
        set[whichbutton].singleclicked = whichfunction;
5 Plagman 278
}
279
 
6334 terminx 280
void CONTROL_MapAnalogAxis(int whichaxis, int whichanalog, controldevice device)
5 Plagman 281
{
289 terminx 282
    controlaxismaptype *set;
5 Plagman 283
 
7943 hendricks2 284
    if ((unsigned)whichanalog >= (unsigned)analog_maxtype && whichanalog != -1)
809 terminx 285
    {
289 terminx 286
        //Error("CONTROL_MapAnalogAxis: analog function %d out of valid range for %d analog functions.",
287
        //              whichanalog, analog_maxtype);
288
        return;
289
    }
5 Plagman 290
 
809 terminx 291
    switch (device)
292
    {
289 terminx 293
    case controldevice_joystick:
6334 terminx 294
        if ((unsigned)whichaxis >= (unsigned)MAXJOYAXES)
809 terminx 295
        {
289 terminx 296
            //Error("CONTROL_MapAnalogAxis: axis %d out of valid range for %d joystick axes.",
297
            //          whichaxis, MAXJOYAXES);
298
            return;
299
        }
5 Plagman 300
 
289 terminx 301
        set = CONTROL_JoyAxesMap;
302
        break;
5 Plagman 303
 
289 terminx 304
    default:
305
        //Error("CONTROL_MapAnalogAxis: invalid controller device type");
306
        return;
307
    }
5 Plagman 308
 
289 terminx 309
    set[whichaxis].analogmap = whichanalog;
5 Plagman 310
}
311
 
1346 terminx 312
void CONTROL_SetAnalogAxisScale(int32_t whichaxis, int32_t axisscale, controldevice device)
5 Plagman 313
{
1346 terminx 314
    int32_t *set;
5 Plagman 315
 
809 terminx 316
    switch (device)
317
    {
289 terminx 318
    case controldevice_mouse:
8127 terminx 319
        if ((unsigned) whichaxis >= ARRAY_SIZE(CONTROL_MouseAxesScale))
809 terminx 320
        {
289 terminx 321
            //Error("CONTROL_SetAnalogAxisScale: axis %d out of valid range for %d mouse axes.",
322
            //          whichaxis, MAXMOUSEAXES);
323
            return;
324
        }
5 Plagman 325
 
289 terminx 326
        set = CONTROL_MouseAxesScale;
327
        break;
5 Plagman 328
 
289 terminx 329
    case controldevice_joystick:
6334 terminx 330
        if ((unsigned) whichaxis >= (unsigned) MAXJOYAXES)
809 terminx 331
        {
289 terminx 332
            //Error("CONTROL_SetAnalogAxisScale: axis %d out of valid range for %d joystick axes.",
333
            //          whichaxis, MAXJOYAXES);
334
            return;
335
        }
5 Plagman 336
 
289 terminx 337
        set = CONTROL_JoyAxesScale;
338
        break;
5 Plagman 339
 
289 terminx 340
    default:
341
        //Error("CONTROL_SetAnalogAxisScale: invalid controller device type");
342
        return;
343
    }
344
 
345
    set[whichaxis] = axisscale;
5 Plagman 346
}
347
 
7986 hendricks2 348
void CONTROL_SetAnalogAxisInvert(int32_t whichaxis, int32_t invert, controldevice device)
349
{
350
    int8_t * set;
351
 
352
    switch (device)
353
    {
354
    case controldevice_joystick:
355
        if ((unsigned) whichaxis >= (unsigned) MAXJOYAXES)
356
        {
357
            //Error("CONTROL_SetAnalogAxisInvert: axis %d out of valid range for %d joystick axes.",
358
            //          whichaxis, MAXJOYAXES);
359
            return;
360
        }
361
 
362
        set = CONTROL_JoyAxesInvert;
363
        break;
364
 
365
    default:
366
        //Error("CONTROL_SetAnalogAxisInvert: invalid controller device type");
367
        return;
368
    }
369
 
370
    set[whichaxis] = invert;
371
}
372
 
1346 terminx 373
void CONTROL_MapDigitalAxis(int32_t whichaxis, int32_t whichfunction, int32_t direction, controldevice device)
5 Plagman 374
{
289 terminx 375
    controlaxismaptype *set;
5 Plagman 376
 
289 terminx 377
    if (CONTROL_CheckRange(whichfunction)) whichfunction = AXISUNDEFINED;
5 Plagman 378
 
809 terminx 379
    switch (device)
380
    {
289 terminx 381
    case controldevice_joystick:
6334 terminx 382
        if ((unsigned) whichaxis >= (unsigned) MAXJOYAXES)
809 terminx 383
        {
289 terminx 384
            //Error("CONTROL_MapDigitalAxis: axis %d out of valid range for %d joystick axes.",
385
            //          whichaxis, MAXJOYAXES);
386
            return;
387
        }
5 Plagman 388
 
289 terminx 389
        set = CONTROL_JoyAxesMap;
390
        break;
5 Plagman 391
 
289 terminx 392
    default:
393
        //Error("CONTROL_MapDigitalAxis: invalid controller device type");
394
        return;
395
    }
396
 
809 terminx 397
    switch (direction)          // JBF: this is all very much a guess. The ASM puzzles me.
398
    {
289 terminx 399
    case axis_up:
400
    case axis_left:
401
        set[whichaxis].minmap = whichfunction;
402
        break;
403
    case axis_down:
404
    case axis_right:
405
        set[whichaxis].maxmap = whichfunction;
406
        break;
407
    default:
408
        break;
409
    }
5 Plagman 410
}
411
 
809 terminx 412
void CONTROL_ClearAssignments(void)
5 Plagman 413
{
7181 terminx 414
    memset(CONTROL_JoyAxes,             0,               sizeof(CONTROL_JoyAxes));
7986 hendricks2 415
    memset(CONTROL_JoyAxesInvert,       0,               sizeof(CONTROL_JoyAxesInvert));
7181 terminx 416
    memset(CONTROL_JoyAxesMap,          AXISUNDEFINED,   sizeof(CONTROL_JoyAxesMap));
289 terminx 417
    memset(CONTROL_JoyButtonMapping,    BUTTONUNDEFINED, sizeof(CONTROL_JoyButtonMapping));
3124 helixhorne 418
//    memset(CONTROL_KeyMapping,          KEYUNDEFINED,    sizeof(CONTROL_KeyMapping));
7181 terminx 419
    memset(CONTROL_LastJoyAxes,         0,               sizeof(CONTROL_LastJoyAxes));
420
    memset(CONTROL_MouseButtonMapping,  BUTTONUNDEFINED, sizeof(CONTROL_MouseButtonMapping));
421
 
422
    for (int & i : CONTROL_MouseAxesScale)
423
        i = NORMALAXISSCALE;
424
 
425
    for (int & i : CONTROL_JoyAxesScale)
426
        i = NORMALAXISSCALE;
5 Plagman 427
}
428
 
7992 hendricks2 429
static int DoGetDeviceButtons(
1346 terminx 430
    int32_t buttons, int32_t tm,
431
    int32_t NumButtons,
432
    int32_t *DeviceButtonState,
433
    int32_t *ButtonClickedTime,
434
    int32_t *ButtonClickedState,
435
    int32_t *ButtonClicked,
436
    uint8_t *ButtonClickedCount
809 terminx 437
)
438
{
2940 helixhorne 439
    int32_t i=NumButtons-1;
7992 hendricks2 440
    int retval = 0;
5 Plagman 441
 
1346 terminx 442
    for (; i>=0; i--)
809 terminx 443
    {
7181 terminx 444
        int const bs = (buttons >> i) & 1;
5 Plagman 445
 
7181 terminx 446
        DeviceButtonState[i]  = bs;
1346 terminx 447
        ButtonClickedState[i] = FALSE;
5 Plagman 448
 
809 terminx 449
        if (bs)
450
        {
7992 hendricks2 451
            retval = 1;
452
 
1346 terminx 453
            if (ButtonClicked[i] == FALSE)
809 terminx 454
            {
1346 terminx 455
                ButtonClicked[i] = TRUE;
5 Plagman 456
 
809 terminx 457
                if (ButtonClickedCount[i] == 0 || tm > ButtonClickedTime[i])
458
                {
7181 terminx 459
                    ButtonClickedTime[i]  = tm + CONTROL_DoubleClickSpeed;
289 terminx 460
                    ButtonClickedCount[i] = 1;
461
                }
809 terminx 462
                else if (tm < ButtonClickedTime[i])
463
                {
1346 terminx 464
                    ButtonClickedState[i] = TRUE;
289 terminx 465
                    ButtonClickedTime[i]  = 0;
466
                    ButtonClickedCount[i] = 2;
467
                }
468
            }
809 terminx 469
            else if (ButtonClickedCount[i] == 2)
470
            {
1346 terminx 471
                ButtonClickedState[i] = TRUE;
289 terminx 472
            }
2940 helixhorne 473
 
1015 terminx 474
            continue;
809 terminx 475
        }
2940 helixhorne 476
 
1015 terminx 477
        if (ButtonClickedCount[i] == 2)
478
            ButtonClickedCount[i] = 0;
5 Plagman 479
 
1346 terminx 480
        ButtonClicked[i] = FALSE;
289 terminx 481
    }
7992 hendricks2 482
 
483
    return retval;
5 Plagman 484
}
485
 
3124 helixhorne 486
static void CONTROL_GetDeviceButtons(void)
5 Plagman 487
{
7181 terminx 488
    int32_t const t = ExtGetTime();
5 Plagman 489
 
809 terminx 490
    if (CONTROL_MouseEnabled)
491
    {
289 terminx 492
        DoGetDeviceButtons(
493
            MOUSE_GetButtons(), t,
494
            CONTROL_NumMouseButtons,
495
            CONTROL_MouseButtonState,
496
            CONTROL_MouseButtonClickedTime,
497
            CONTROL_MouseButtonClickedState,
498
            CONTROL_MouseButtonClicked,
499
            CONTROL_MouseButtonClickedCount
500
        );
501
    }
5 Plagman 502
 
809 terminx 503
    if (CONTROL_JoystickEnabled)
504
    {
7992 hendricks2 505
        int retval = DoGetDeviceButtons(
6334 terminx 506
            JOYSTICK_GetButtons(), t,
289 terminx 507
            CONTROL_NumJoyButtons,
508
            CONTROL_JoyButtonState,
509
            CONTROL_JoyButtonClickedTime,
510
            CONTROL_JoyButtonClickedState,
511
            CONTROL_JoyButtonClicked,
512
            CONTROL_JoyButtonClickedCount
513
        );
7992 hendricks2 514
        if (retval)
515
            CONTROL_LastSeenInput = LastSeenInput::Joystick;
289 terminx 516
    }
5 Plagman 517
}
518
 
7992 hendricks2 519
static int CONTROL_DigitizeAxis(int axis, controldevice device)
5 Plagman 520
{
289 terminx 521
    controlaxistype *set, *lastset;
5 Plagman 522
 
809 terminx 523
    switch (device)
524
    {
289 terminx 525
    case controldevice_joystick:
526
        set = CONTROL_JoyAxes;
527
        lastset = CONTROL_LastJoyAxes;
528
        break;
529
 
7992 hendricks2 530
    default: return 0;
289 terminx 531
    }
532
 
7970 hendricks2 533
    set[axis].digitalClearedN = lastset[axis].digitalClearedN;
534
    set[axis].digitalClearedP = lastset[axis].digitalClearedP;
535
 
809 terminx 536
    if (set[axis].analog > 0)
537
    {
7970 hendricks2 538
        set[axis].digitalClearedN = 0;
539
 
1018 terminx 540
        if (set[axis].analog > THRESHOLD || (set[axis].analog > MINTHRESHOLD && lastset[axis].digital == 1))
541
            set[axis].digital = 1;
7970 hendricks2 542
        else
543
            set[axis].digitalClearedP = 0;
7992 hendricks2 544
 
545
        return 1;
809 terminx 546
    }
7970 hendricks2 547
    else if (set[axis].analog < 0)
809 terminx 548
    {
7970 hendricks2 549
        set[axis].digitalClearedP = 0;
550
 
1018 terminx 551
        if (set[axis].analog < -THRESHOLD || (set[axis].analog < -MINTHRESHOLD && lastset[axis].digital == -1))
289 terminx 552
            set[axis].digital = -1;
7970 hendricks2 553
        else
554
            set[axis].digitalClearedN = 0;
7992 hendricks2 555
 
556
        return 1;
289 terminx 557
    }
7970 hendricks2 558
    else
559
    {
560
        set[axis].digitalClearedN = 0;
561
        set[axis].digitalClearedP = 0;
562
    }
7992 hendricks2 563
 
564
    return 0;
5 Plagman 565
}
566
 
7181 terminx 567
static void CONTROL_ScaleAxis(int axis, controldevice device)
5 Plagman 568
{
289 terminx 569
    controlaxistype *set;
1346 terminx 570
    int32_t *scale;
7986 hendricks2 571
    int8_t * invert;
5 Plagman 572
 
809 terminx 573
    switch (device)
574
    {
289 terminx 575
    case controldevice_joystick:
576
        set = CONTROL_JoyAxes;
577
        scale = CONTROL_JoyAxesScale;
7986 hendricks2 578
        invert = CONTROL_JoyAxesInvert;
289 terminx 579
        break;
5 Plagman 580
 
289 terminx 581
    default: return;
582
    }
583
 
7986 hendricks2 584
    int const invertResult = !!invert[axis];
585
    set[axis].analog = (mulscale16(set[axis].analog, scale[axis]) ^ -invertResult) + invertResult;
5 Plagman 586
}
587
 
7181 terminx 588
static void CONTROL_ApplyAxis(int axis, ControlInfo *info, controldevice device)
5 Plagman 589
{
289 terminx 590
    controlaxistype *set;
591
    controlaxismaptype *map;
5 Plagman 592
 
809 terminx 593
    switch (device)
594
    {
289 terminx 595
    case controldevice_joystick:
596
        set = CONTROL_JoyAxes;
597
        map = CONTROL_JoyAxesMap;
598
        break;
5 Plagman 599
 
289 terminx 600
    default: return;
601
    }
602
 
809 terminx 603
    switch (map[axis].analogmap)
604
    {
289 terminx 605
    case analog_turning:          info->dyaw   += set[axis].analog; break;
606
    case analog_strafing:         info->dx     += set[axis].analog; break;
607
    case analog_lookingupanddown: info->dpitch += set[axis].analog; break;
608
    case analog_elevation:        info->dy     += set[axis].analog; break;
609
    case analog_rolling:          info->droll  += set[axis].analog; break;
610
    case analog_moving:           info->dz     += set[axis].analog; break;
611
    default: break;
612
    }
5 Plagman 613
}
614
 
2940 helixhorne 615
static void CONTROL_PollDevices(ControlInfo *info)
5 Plagman 616
{
289 terminx 617
    memset(info, 0, sizeof(ControlInfo));
5 Plagman 618
 
4386 terminx 619
#ifdef __ANDROID__
620
    CONTROL_Android_PollDevices(info);
621
#endif
622
 
809 terminx 623
    if (CONTROL_MouseEnabled)
7947 hendricks2 624
        CONTROL_GetMouseDelta(info);
1014 terminx 625
 
809 terminx 626
    if (CONTROL_JoystickEnabled)
627
    {
3200 terminx 628
        Bmemcpy(CONTROL_LastJoyAxes,   CONTROL_JoyAxes,   sizeof(CONTROL_JoyAxes));
629
        memset(CONTROL_JoyAxes,   0, sizeof(CONTROL_JoyAxes));
630
 
7181 terminx 631
        for (int i=joystick.numAxes-1; i>=0; i--)
809 terminx 632
        {
6827 terminx 633
            CONTROL_JoyAxes[i].analog = joystick.pAxis[i];
3196 terminx 634
 
7992 hendricks2 635
            if (CONTROL_DigitizeAxis(i, controldevice_joystick))
636
                CONTROL_LastSeenInput = LastSeenInput::Joystick;
289 terminx 637
            CONTROL_ScaleAxis(i, controldevice_joystick);
638
            LIMITCONTROL(&CONTROL_JoyAxes[i].analog);
639
            CONTROL_ApplyAxis(i, info, controldevice_joystick);
1017 terminx 640
        }
289 terminx 641
    }
642
 
643
    CONTROL_GetDeviceButtons();
5 Plagman 644
}
645
 
7992 hendricks2 646
static int CONTROL_HandleAxisFunction(int32_t *p1, controlaxistype *axes, controlaxismaptype *axismap, int numAxes)
5 Plagman 647
{
7181 terminx 648
    int axis = numAxes - 1;
7992 hendricks2 649
    int retval = 0;
7181 terminx 650
 
651
    do
809 terminx 652
    {
7181 terminx 653
        if (!axes[axis].digital)
654
            continue;
5 Plagman 655
 
7181 terminx 656
        int const j = (axes[axis].digital < 0) ? axismap[axis].minmap : axismap[axis].maxmap;
5 Plagman 657
 
7181 terminx 658
        if (j != AXISUNDEFINED)
7992 hendricks2 659
        {
7181 terminx 660
            p1[j] = 1;
7992 hendricks2 661
            retval = 1;
662
        }
1016 terminx 663
    }
7181 terminx 664
    while (axis--);
7992 hendricks2 665
 
666
    return retval;
7181 terminx 667
}
1014 terminx 668
 
7181 terminx 669
static void CONTROL_AxisFunctionState(int32_t *p1)
670
{
1016 terminx 671
    if (CONTROL_NumJoyAxes)
7992 hendricks2 672
    {
673
        if (CONTROL_HandleAxisFunction(p1, CONTROL_JoyAxes, CONTROL_JoyAxesMap, CONTROL_NumJoyAxes))
674
            CONTROL_LastSeenInput = LastSeenInput::Joystick;
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
    {
7181 terminx 682
        int i = CONTROL_NumMouseButtons-1, j;
5 Plagman 683
 
1018 terminx 684
        do
826 terminx 685
        {
6328 terminx 686
            if (!CONTROL_KeyBinds[MAXBOUNDKEYS + i].cmdstr)
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
 
6328 terminx 700
            if (CONTROL_KeyBinds[MAXBOUNDKEYS + i].cmdstr && CONTROL_MouseButtonState[i])
1018 terminx 701
            {
6328 terminx 702
                if (CONTROL_KeyBinds[MAXBOUNDKEYS + i].repeat || (CONTROL_KeyBinds[MAXBOUNDKEYS + i].laststate == 0))
703
                    OSD_Dispatch(CONTROL_KeyBinds[MAXBOUNDKEYS + i].cmdstr);
1018 terminx 704
            }
6328 terminx 705
            CONTROL_KeyBinds[MAXBOUNDKEYS + 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
    {
7181 terminx 712
        int i=CONTROL_NumJoyButtons-1, j;
7992 hendricks2 713
        int retval = 0;
5 Plagman 714
 
1018 terminx 715
        do
716
        {
717
            j = CONTROL_JoyButtonMapping[i].doubleclicked;
718
            if (j != KEYUNDEFINED)
7992 hendricks2 719
            {
720
                auto const state = CONTROL_JoyButtonClickedState[i];
721
                p1[j] |= state;
722
                retval |= state;
723
            }
1018 terminx 724
 
725
            j = CONTROL_JoyButtonMapping[i].singleclicked;
726
            if (j != KEYUNDEFINED)
7992 hendricks2 727
            {
728
                auto const state = CONTROL_JoyButtonState[i];
729
                p1[j] |= state;
730
                retval |= state;
731
            }
1018 terminx 732
        }
1023 terminx 733
        while (i--);
7992 hendricks2 734
 
735
        if (retval)
736
            CONTROL_LastSeenInput = LastSeenInput::Joystick;
1017 terminx 737
    }
5 Plagman 738
}
739
 
7181 terminx 740
void CONTROL_ClearButton(int whichbutton)
5 Plagman 741
{
5625 terminx 742
    if (CONTROL_CheckRange(whichbutton)) return;
4440 terminx 743
 
744
#ifdef __ANDROID__
745
    CONTROL_Android_ClearButton(whichbutton);
5625 terminx 746
#endif
747
 
809 terminx 748
    BUTTONCLEAR(whichbutton);
1346 terminx 749
    CONTROL_Flags[whichbutton].cleared = TRUE;
5 Plagman 750
}
751
 
7898 hendricks2 752
void CONTROL_ClearAllButtons(void)
753
{
754
    CONTROL_ButtonHeldState = 0;
755
    CONTROL_ButtonState = 0;
756
 
757
    for (auto & c : CONTROL_Flags)
758
        c.cleared = TRUE;
759
}
760
 
7970 hendricks2 761
int32_t CONTROL_GetGameControllerDigitalAxisPos(int32_t axis)
762
{
763
    if (!joystick.isGameController)
764
        return 0;
765
 
766
    return CONTROL_JoyAxes[axis].digital > 0 && !CONTROL_JoyAxes[axis].digitalClearedP;
767
}
768
int32_t CONTROL_GetGameControllerDigitalAxisNeg(int32_t axis)
769
{
770
    if (!joystick.isGameController)
771
        return 0;
772
 
773
    return CONTROL_JoyAxes[axis].digital < 0 && !CONTROL_JoyAxes[axis].digitalClearedN;
774
}
775
 
776
void CONTROL_ClearGameControllerDigitalAxisPos(int32_t axis)
777
{
778
    if (!joystick.isGameController)
779
        return;
780
 
781
    CONTROL_JoyAxes[axis].digitalClearedP = 1;
782
}
783
void CONTROL_ClearGameControllerDigitalAxisNeg(int32_t axis)
784
{
785
    if (!joystick.isGameController)
786
        return;
787
 
788
    CONTROL_JoyAxes[axis].digitalClearedN = 1;
789
}
790
 
1632 terminx 791
void CONTROL_ProcessBinds(void)
808 terminx 792
{
3195 terminx 793
    if (!CONTROL_BindsEnabled)
1425 terminx 794
        return;
808 terminx 795
 
7181 terminx 796
    int i = MAXBOUNDKEYS-1;
797
 
1632 terminx 798
    do
808 terminx 799
    {
3197 terminx 800
        if (CONTROL_KeyBinds[i].cmdstr)
808 terminx 801
        {
7992 hendricks2 802
            auto const keyPressed = KB_KeyPressed(i);
803
 
804
            if (keyPressed && (CONTROL_KeyBinds[i].repeat || (CONTROL_KeyBinds[i].laststate == 0)))
805
            {
806
                CONTROL_LastSeenInput = LastSeenInput::Keyboard;
3197 terminx 807
                OSD_Dispatch(CONTROL_KeyBinds[i].cmdstr);
7992 hendricks2 808
            }
3127 helixhorne 809
 
7992 hendricks2 810
            CONTROL_KeyBinds[i].laststate = keyPressed;
808 terminx 811
        }
1017 terminx 812
    }
1632 terminx 813
    while (i--);
808 terminx 814
}
815
 
2940 helixhorne 816
static void CONTROL_GetFunctionInput(void)
5 Plagman 817
{
7181 terminx 818
    CONTROL_ButtonFunctionState(CONTROL_ButtonFlags);
819
    CONTROL_AxisFunctionState(CONTROL_ButtonFlags);
5 Plagman 820
 
1144 terminx 821
    CONTROL_ButtonHeldState = CONTROL_ButtonState;
822
    CONTROL_ButtonState = 0;
5 Plagman 823
 
7181 terminx 824
    int i = CONTROL_NUM_FLAGS-1;
825
 
1632 terminx 826
    do
809 terminx 827
    {
8303 hendricks2 828
        CONTROL_SetFlag(i, CONTROL_KeyboardFunctionPressed(i) | CONTROL_ButtonFlags[i]);
5 Plagman 829
 
1632 terminx 830
        if (CONTROL_Flags[i].cleared == FALSE) BUTTONSET(i, CONTROL_Flags[i].active);
831
        else if (CONTROL_Flags[i].active == FALSE) CONTROL_Flags[i].cleared = 0;
289 terminx 832
    }
1632 terminx 833
    while (i--);
1014 terminx 834
 
7181 terminx 835
    memset(CONTROL_ButtonFlags, 0, sizeof(CONTROL_ButtonFlags));
5 Plagman 836
}
837
 
2728 hendricks2 838
void CONTROL_GetInput(ControlInfo *info)
839
{
4440 terminx 840
#ifdef __ANDROID__
841
    CONTROL_Android_PollDevices(info);
7181 terminx 842
#endif
2728 hendricks2 843
    CONTROL_PollDevices(info);
844
    CONTROL_GetFunctionInput();
4200 hendricks2 845
    inputchecked = 1;
2728 hendricks2 846
}
847
 
7985 hendricks2 848
static void CONTROL_ResetJoystickValues()
849
{
850
    CONTROL_NumJoyAxes      = min(MAXJOYAXES, joystick.numAxes);
851
    CONTROL_NumJoyButtons   = min(MAXJOYBUTTONS, joystick.numButtons + 4 * (joystick.numHats > 0));
852
    CONTROL_JoystickEnabled = CONTROL_JoyPresent = !!((inputdevices & 4) >> 2);
853
}
854
 
855
void CONTROL_ScanForControllers()
856
{
857
    joyScanDevices();
858
    CONTROL_ResetJoystickValues();
859
}
860
 
7181 terminx 861
bool CONTROL_Startup(controltype which, int32_t(*TimeFunction)(void), int32_t ticspersecond)
5 Plagman 862
{
654 terminx 863
    UNREFERENCED_PARAMETER(which);
649 terminx 864
 
7181 terminx 865
    if (CONTROL_Started) return false;
5 Plagman 866
 
7181 terminx 867
    ExtGetTime = TimeFunction ? TimeFunction : CONTROL_GetTime;
5 Plagman 868
 
7181 terminx 869
    // what the fuck???
870
    CONTROL_DoubleClickSpeed = (ticspersecond * 57) / 100;
5 Plagman 871
 
289 terminx 872
    if (CONTROL_DoubleClickSpeed <= 0)
873
        CONTROL_DoubleClickSpeed = 1;
5 Plagman 874
 
8005 terminx 875
    if (initinput())
7181 terminx 876
        return true;
5 Plagman 877
 
289 terminx 878
    KB_Startup();
5 Plagman 879
 
7181 terminx 880
    CONTROL_NumMouseButtons = MAXMOUSEBUTTONS;
881
    CONTROL_MousePresent    = Mouse_Init();
882
    CONTROL_MouseEnabled    = CONTROL_MousePresent;
5 Plagman 883
 
7985 hendricks2 884
    CONTROL_ResetJoystickValues();
5 Plagman 885
 
2624 helixhorne 886
#ifdef GEKKO
289 terminx 887
    if (CONTROL_MousePresent)
888
        initprintf("CONTROL_Startup: Mouse Present\n");
7181 terminx 889
 
289 terminx 890
    if (CONTROL_JoyPresent)
891
        initprintf("CONTROL_Startup: Joystick Present\n");
2334 helixhorne 892
#endif
5 Plagman 893
 
1144 terminx 894
    CONTROL_ButtonState     = 0;
895
    CONTROL_ButtonHeldState = 0;
289 terminx 896
 
7181 terminx 897
    for (auto & CONTROL_Flag : CONTROL_Flags)
898
        CONTROL_Flag.used = FALSE;
289 terminx 899
 
1346 terminx 900
    CONTROL_Started = TRUE;
289 terminx 901
 
7181 terminx 902
    return false;
5 Plagman 903
}
904
 
905
void CONTROL_Shutdown(void)
906
{
3209 helixhorne 907
    if (!CONTROL_Started)
908
        return;
3197 terminx 909
 
3209 helixhorne 910
    CONTROL_ClearAllBinds();
5 Plagman 911
 
289 terminx 912
    MOUSE_Shutdown();
913
    uninitinput();
5 Plagman 914
 
1346 terminx 915
    CONTROL_Started = FALSE;
5 Plagman 916
}
917
 
8293 hendricks2 918
 
919
// temporary hack until input is unified
920
void CONTROL_GetUserInput(UserInput * uinfo)
921
{
922
    if (
923
        KB_KeyPressed(sc_DownArrow)
924
        || KB_KeyPressed(sc_kpad_2)
925
        || (MOUSE_GetButtons()&WHEELDOWN_MOUSE)
926
        )
927
        uinfo->dir = dir_South;
928
    else if (
929
        KB_KeyPressed(sc_UpArrow)
930
        || KB_KeyPressed(sc_kpad_8)
931
        || (MOUSE_GetButtons()&WHEELUP_MOUSE)
932
        )
933
        uinfo->dir = dir_North;
934
    else if (
935
        KB_KeyPressed(sc_LeftArrow)
936
        || KB_KeyPressed(sc_kpad_4)
937
        )
938
        uinfo->dir = dir_West;
939
    else if (
940
        KB_KeyPressed(sc_RightArrow)
941
        || KB_KeyPressed(sc_kpad_6)
942
        )
943
        uinfo->dir = dir_East;
944
 
945
    uinfo->button0 =
946
        KB_KeyPressed(sc_Enter)
947
        || KB_KeyPressed(sc_kpad_Enter)
948
        || (MOUSE_GetButtons()&LEFT_MOUSE)
949
        ;
950
 
951
    uinfo->button1 =
952
        KB_KeyPressed(sc_Escape)
953
        || (MOUSE_GetButtons()&RIGHT_MOUSE)
954
        ;
955
}
956
void CONTROL_ClearUserInput(UserInput * uinfo)
957
{
8486 hendricks2 958
    UNREFERENCED_PARAMETER(uinfo);
959
 
8341 hendricks2 960
    KB_FlushKeyboardQueue();
961
 
962
    KB_ClearKeyDown(sc_UpArrow);
963
    KB_ClearKeyDown(sc_kpad_8);
964
    MOUSE_ClearButton(WHEELUP_MOUSE);
965
 
966
    KB_ClearKeyDown(sc_DownArrow);
967
    KB_ClearKeyDown(sc_kpad_2);
968
    MOUSE_ClearButton(WHEELDOWN_MOUSE);
969
 
970
    KB_ClearKeyDown(sc_LeftArrow);
971
    KB_ClearKeyDown(sc_kpad_4);
972
 
973
    KB_ClearKeyDown(sc_RightArrow);
974
    KB_ClearKeyDown(sc_kpad_6);
975
 
976
    KB_ClearKeyDown(sc_kpad_Enter);
977
    KB_ClearKeyDown(sc_Enter);
978
    MOUSE_ClearButton(LEFT_MOUSE);
979
 
980
    KB_ClearKeyDown(sc_Escape);
981
    MOUSE_ClearButton(RIGHT_MOUSE);
8293 hendricks2 982
}