summaryrefslogtreecommitdiff
path: root/arm9/source/devices/input.c
blob: e5ac5bed3ea6f0ee7cfdc8eae734b7d44fbeaf0e (plain) (blame)
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
#include <nds.h>
#include "../bang.h"
#include "input.h"

void inp_receive_byte(InputDevice *inp, u8 byte) {
    cb_write_byte(&inp->keybuffer, byte);
    inp->wake = true;
}

// Read gamepad state into an input device.
void inp_read_gamepad(InputDevice *inp) {
    u32 held = keysHeld();
    u8 gamepad = (
        TEST(held, KEY_UP)    << 7
      | TEST(held, KEY_DOWN)  << 6
      | TEST(held, KEY_LEFT)  << 5
      | TEST(held, KEY_RIGHT) << 4
      | TEST(held, KEY_A)     << 3
      | TEST(held, KEY_B)     << 2
      | TEST(held, KEY_X)     << 1
      | TEST(held, KEY_Y)     << 0
    );
    if (gamepad != inp->gamepad) {
        inp->wake = TRUE;
        inp->gamepad = gamepad;
    }
}

// Read navigation state into an input device.
void inp_read_navigation(InputDevice *inp) {
    u32 held = keysHeld();
    u8 navigation = (
        TEST(held, KEY_UP)    << 7
      | TEST(held, KEY_DOWN)  << 6
      | TEST(held, KEY_LEFT)  << 5
      | TEST(held, KEY_RIGHT) << 4
      | TEST(held, KEY_A)     << 3
      | TEST(held, KEY_B)     << 2
      | TEST(held, KEY_R)     << 1
      | TEST(held, KEY_L)     << 0
    );
    if (navigation != inp->navigation) {
        inp->wake = TRUE;
        inp->navigation = navigation;
    }
}

// Read touchscreen state into an input device.
void inp_read_touch(InputDevice *inp) {
    bool pointer = TEST(keysHeld(), KEY_TOUCH);
    if (pointer != inp->pointer) {
        inp->wake = TRUE;
        inp->pointer = pointer;
    }
    if (pointer) {
        touchPosition pos;
        touchRead(&pos);
        if (pos.px != inp->x || pos.py != inp->y) {
            inp->wake = TRUE;
            inp->x = pos.px;
            inp->y = pos.py;
        }
    }
}