Subversion Repositories eduke32

Rev

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