Subversion Repositories eduke32

Rev

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