aboutsummaryrefslogtreecommitdiff
path: root/sdk-modifications/libsrc/key/key.c
blob: 8a6ad5f92e86515778f545d17fc87eb42376a36b (plain)
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
#include "ds2_timer.h"
#include "ds2io.h"

#define KEY_REPEAT_TIME		200		//5 times/second
#define KEY_REPEAT_TIME_N	(KEY_REPEAT_TIME*1000000/SYSTIME_UNIT)

#define INPUT_REPEAT_TIME	100
#define INPUT_REPEAT_TIME_N (INPUT_REPEAT_TIME*1000000/SYSTIME_UNIT)

static unsigned int _last_key = 0;
static unsigned int _last_key_timestamp = 0;

/*
*   Function: only detect the key pressed or not
*/
unsigned int getKey(void)
{
	struct key_buf input;
	unsigned int new_key, hold_key, ret_key, timestamp;
	int flag;

	ds2_getrawInput(&input);
	timestamp = getSysTime();
	flag = ((timestamp - _last_key_timestamp) >= KEY_REPEAT_TIME_N) ? 1 : 0;

	input.key &= 0x3FFF;
	new_key = (_last_key ^ input.key) & input.key;
	hold_key = _last_key & input.key;

	ret_key = 0;
	if(hold_key) {
		if(flag)
		{
			ret_key = hold_key;
			_last_key_timestamp = timestamp;
		}
	}

	if(new_key)
	{
		ret_key |= new_key;
		_last_key_timestamp = timestamp;

		_last_key = input.key;
	}

	return ret_key;
}

/*
*   Function: can get the detail information about key pressed, hold, or release
*/
unsigned int getKey1(void)
{
	struct key_buf input;
	unsigned int new_key, hold_key, ret_key, timestamp;
	int flag;

	ds2_getrawInput(&input);
	timestamp = getSysTime();
	flag = ((timestamp - _last_key_timestamp) >= KEY_REPEAT_TIME_N) ? 1 : 0;

	input.key &= 0x3FFF;
	new_key = _last_key ^ input.key;
	hold_key = _last_key & input.key;
	//ret_key[31:16] = last key;
	//ret_key[15:0]	= new key;
	
	ret_key = 0;
	if(hold_key) {
		if(flag)
		{
			ret_key = (hold_key << 16) | hold_key;
			_last_key_timestamp = timestamp;
		}
	}

	if(new_key)
	{
		unsigned int tmp_key;

		//have new key pressed
		if(new_key & input.key)
		{
			ret_key |= new_key & input.key;
			_last_key_timestamp = timestamp;
		}
		else
		{
			ret_key |= (new_key & ~input.key) << 16;
		}

		_last_key = input.key;
	}

	return ret_key;
}

static unsigned int _last_input = 0;
static unsigned int _last_input_timestamp = 0;

/*
*   Function: only detect the key pressed or not and touch position
*/
unsigned int getInput(struct key_buf *input) {
    struct key_buf rawin;
    unsigned int timestamp, new, hold;
    int flag, ret;

	ds2_getrawInput(&rawin);
	timestamp = getSysTime();
	flag = ((timestamp - _last_input_timestamp) >= INPUT_REPEAT_TIME_N) ? 1 : 0;
    
    rawin.key &= 0x3FFF;
    new = (rawin.key ^ _last_input) & rawin.key;
    hold = rawin.key & _last_input;
    
    ret = 0;
    if(hold && flag)
    {
        ret = hold;
        input->x = rawin.x;
        input->y = rawin.y;
        
        _last_input_timestamp = timestamp;
    }
    
    if(new)
    {
        ret |= new;
        input->x = rawin.x;
        input->y = rawin.y;
        
        _last_input_timestamp = timestamp;
        _last_input = rawin.key;
    }
    
    input->key = ret;
    return ret > 0;
}