Subversion Repositories eduke32

Rev

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