1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use super::{
    type_conversion::{BYTE, DWORD, LONG, ULONG},
    VJDButtonState,
};

/**
    Holds data that describes a position of a vJoy device.

    The data layout and data types follows what is set in the vJoy C API.
*/
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct PositionV2 {
    /**
        Device id (1-based).

        Note that the internal implementation of
        `UpdateVJD(UINT rID, PVOID pData)` in the C API will for some reason
        always reassign this field to the value received in `rID`, so any change we make won't be saved in practice...
    */
    bDevice: BYTE,

    wThrottle: LONG,
    wRudder: LONG,
    wAileron: LONG,

    /// Axis X
    wAxisX: LONG,

    /// Axis Y
    wAxisY: LONG,

    /// Axis Z
    wAxisZ: LONG,

    /// Axis X Rotation
    wAxisXRot: LONG,

    /// Axis Y Rotation
    wAxisYRot: LONG,

    /// Axis Z Rotation
    wAxisZRot: LONG,

    /// Slider 1
    wSlider: LONG,

    /// Slider 2
    wDial: LONG,

    wWheel: LONG,
    wAxisVX: LONG,
    wAxisVY: LONG,
    wAxisVZ: LONG,
    wAxisVBRX: LONG,
    wAxisVBRY: LONG,
    wAxisVBRZ: LONG,

    /**
        Buttons 1-32. Each bit position represents a button state. Ascending button number from right to left.

        0b00000000000000000000000000000001 (0x00000001) -> button1  is pressed.
        0b10000000000000000000000000000000 (0x80000000) -> button32 is pressed.
    */
    lButtons: ULONG,

    /**
        Continuous-type POV: lower 16 bits, POV #1

        Discrete-type POV: lower 16 bits is divided in 4 chunks of 4 bits each, one
        chunk of 4 bits represents one discrete-type POV. Ascending POV number from right to left:

        0000_0000_0000_0000\
        POV4_POV3_POV2_POV1
    */
    bHats: DWORD,

    /// Continuous-type POV: lower 16 bits, POV #2\
    /// Discrete-type POV: none
    bHatsEx1: DWORD,

    /// Continuous-type POV: lower 16 bits, POV #3\
    /// Discrete-type POV: none
    bHatsEx2: DWORD,

    /// Continuous-type POV: lower 16 bits, POV #4\
    /// Discrete-type POV: none
    bHatsEx3: DWORD,

    /**
        Buttons 33-64. Each bit position represents a button state. Ascending button number from right to left.

        0b00000000000000000000000000000001 (0x00000001) -> button 33 is pressed.
        0b10000000000000000000000000000000 (0x80000000) -> button 64 is pressed.
    */
    lButtonsEx1: ULONG,

    /**
        Buttons 65-96. Each bit position represents a button state. Ascending button number from right to left.

        0b00000000000000000000000000000001 (0x00000001) -> button 65 is pressed.
        0b10000000000000000000000000000000 (0x80000000) -> button 96 is pressed.
    */
    lButtonsEx2: ULONG,

    /**
        Buttons 97-128. Each bit position represents a button state. Ascending button number from right to left.

        0b00000000000000000000000000000001 (0x00000001) -> button 97  is pressed.
        0b10000000000000000000000000000000 (0x80000000) -> button 128 is pressed.
    */
    lButtonsEx3: ULONG,
}

/**
    Holds data that describes a position of a vJoy device. This is a container of information that won't
    do anything until it is send to vJoy.
*/
pub struct VJDPosition {
    device: VJDevice,
    position: PositionV2,
}

impl VJDPosition {
    pub fn new(device: VJDevice) -> VJDPosition {
        VJDPosition {
            device,
            position: PositionV2 {
                bDevice: device as u8,

                wThrottle: 0,
                wRudder: 0,
                wAileron: 0,

                wAxisX: 16384,
                wAxisY: 16384,
                wAxisZ: 16384,

                wAxisXRot: 0,
                wAxisYRot: 0,
                wAxisZRot: 0,

                wSlider: 0,
                wDial: 0,

                wWheel: 0,
                wAxisVX: 0,
                wAxisVY: 0,
                wAxisVZ: 0,
                wAxisVBRX: 0,
                wAxisVBRY: 0,
                wAxisVBRZ: 0,

                // vJoy C API sets neutral to -1 in u32 type which wraps it to the max value by the 2's complement wrapping rule (overflow)
                bHats: u32::MAX,
                bHatsEx1: u32::MAX,
                bHatsEx2: u32::MAX,
                bHatsEx3: u32::MAX,

                lButtons: 0,
                lButtonsEx1: 0,
                lButtonsEx2: 0,
                lButtonsEx3: 0,
            },
        }
    }

    // TODO: doc methods

    pub fn get_device(&self) -> VJDevice {
        self.device
    }

    pub fn get_position(&self) -> PositionV2 {
        self.position
    }

    pub fn set_button(&mut self, button: u32, state: VJDButtonState) {
        match state {
            VJDButtonState::Pressed => self.set_button_pressed(button),
            VJDButtonState::Released => self.set_button_released(button),
        }
    }

    pub fn set_button_pressed(&mut self, button: u32) {
        let mask: u32 = 0b1 << ((button - 1) % 32);

        match (button - 1) / 32 {
            0 => self.position.lButtons |= mask,
            1 => self.position.lButtonsEx1 |= mask,
            2 => self.position.lButtonsEx2 |= mask,
            3 => self.position.lButtonsEx3 |= mask,
            _ => (),
        }
    }

    pub fn set_button_released(&mut self, button: u32) {
        let mut mask: u32 = 0b1 << ((button - 1) % 32);

        mask = !mask;

        match (button - 1) / 32 {
            0 => self.position.lButtons &= mask,
            1 => self.position.lButtonsEx1 &= mask,
            2 => self.position.lButtonsEx2 &= mask,
            3 => self.position.lButtonsEx3 &= mask,
            _ => (),
        }
    }

    pub fn set_disc_pov(&mut self, pov: VJDPovNumber, direction: VJDPovDisc) {
        let shift = 4 * (pov as u32 - 1);

        // Keep all bits except for the POV number given in argument
        self.position.bHats &= !(0b1111 << shift);

        // Inject direction to the POV number given in argument
        self.position.bHats |= (direction as u32) << shift;
    }

    pub fn set_cont_pov(&mut self, pov: VJDPovNumber, value: u32) {
        match pov {
            VJDPovNumber::Pov1 => self.position.bHats = value,
            VJDPovNumber::Pov2 => self.position.bHatsEx1 = value,
            VJDPovNumber::Pov3 => self.position.bHatsEx2 = value,
            VJDPovNumber::Pov4 => self.position.bHatsEx3 = value,
        }
    }

    pub fn set_axis_x(&mut self, value: i32) {
        self.position.wAxisX = value;
    }

    pub fn set_axis_y(&mut self, value: i32) {
        self.position.wAxisY = value;
    }

    pub fn set_axis_z(&mut self, value: i32) {
        self.position.wAxisZ = value;
    }

    pub fn set_axis_xr(&mut self, value: i32) {
        self.position.wAxisXRot = value;
    }

    pub fn set_axis_yr(&mut self, value: i32) {
        self.position.wAxisYRot = value;
    }

    pub fn set_axis_zr(&mut self, value: i32) {
        self.position.wAxisZRot = value;
    }

    pub fn set_slider1(&mut self, value: i32) {
        self.position.wSlider = value;
    }

    pub fn set_slider2(&mut self, value: i32) {
        self.position.wDial = value;
    }
}

/// Describes the status of a vJoy device.
#[repr(C)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum VJDStatus {
    /// The vJoy device is owned by this application.
    Own,

    /// The vJoy device is not owned by any application (including this one).
    Free,

    /// The vJoy device is owned by another application. It cannot be acquired by this
    /// application.
    Busy,

    /// The vJoy device is missing. It either does not exist or the driver is disabled.
    Miss,

    /// Unknown.
    Unknown,
}

/// Describes an axis of a vJoy device.
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum VJDAxis {
    X = 0x30,
    Y = 0x31,
    Z = 0x32,
    Rx = 0x33,
    Ry = 0x34,
    Rz = 0x35,
    Slider1 = 0x36,
    Slider2 = 0x37,
}

// TODO: test it contains device from range [1; MAX]; may need custom macro
/// Describes a vJoy device number ("id").
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum VJDevice {
    // index is 1-based
    /// Device #1.
    D1 = 1,
    /// Device #2.
    D2 = 2,
    /// Device #3.
    D3 = 3,
    /// Device #4.
    D4 = 4,
    /// Device #5.
    D5 = 5,
    /// Device #6.
    D6 = 6,
    /// Device #7.
    D7 = 7,
    /// Device #8.
    D8 = 8,
    /// Device #9.
    D9 = 9,
    /// Device #10.
    D10 = 10,
    /// Device #11.
    D11 = 11,
    /// Device #12.
    D12 = 12,
    /// Device #13.
    D13 = 13,
    /// Device #14.
    D14 = 14,
    /// Device #15.
    D15 = 15,
    /// Device #16.
    D16 = 16,
}
// use std::hash::{Hash, Hasher};

// impl Hash for VJDevice {
//     fn hash<H: Hasher>(&self, state: &mut H) {
//         (*self as u8).hash(state);
//     }
// }

// impl PartialEq for VJDevice {
//     fn eq(&self, other: &VJDevice) -> bool {
//         *self as u8 == *other as u8
//     }
// }

impl VJDevice {
    pub fn get_from(value: u8) -> Option<VJDevice> {
        if !(1..=crate::vjoy_base::driver::VJGeneral::MAX_DEVICES).contains(&value) {
            return None;
        }

        match value {
            1 => Some(VJDevice::D1),
            2 => Some(VJDevice::D2),
            3 => Some(VJDevice::D3),
            4 => Some(VJDevice::D4),
            5 => Some(VJDevice::D5),
            6 => Some(VJDevice::D6),
            7 => Some(VJDevice::D7),
            8 => Some(VJDevice::D8),
            9 => Some(VJDevice::D9),
            10 => Some(VJDevice::D10),
            11 => Some(VJDevice::D11),
            12 => Some(VJDevice::D12),
            13 => Some(VJDevice::D13),
            14 => Some(VJDevice::D14),
            15 => Some(VJDevice::D15),
            16 => Some(VJDevice::D16),
            _ => None,
        }
    }
}

/**
    Describes a discrete POV direction of a vJoy device.
*/
#[repr(i32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum VJDPovDisc {
    Neutral = -1,
    North = 0,
    East = 1,
    South = 2,
    West = 3,
}

/**
    Describes a POV number ("id") of a vJoy device.
*/
#[repr(u8)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum VJDPovNumber {
    /// POV #1.
    Pov1 = 1,
    /// POV #2.
    Pov2 = 2,
    /// POV #3.
    Pov3 = 3,
    /// POV #4.
    Pov4 = 4,
}