Subversion Repositories eduke32

Rev

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