File: keys.c

package info (click to toggle)
lsnipes 0.9.4-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 296 kB
  • ctags: 718
  • sloc: ansic: 4,421; makefile: 84; sh: 10
file content (180 lines) | stat: -rw-r--r-- 3,315 bytes parent folder | download | duplicates (3)
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
#include <stdio.h>
#include <ctype.h>
#include <string.h>

#include "keys.h"


#define MAXLINE	300
#define MAXKEY	40


static char *keystr[] = {
	"c",
	"z",
	"a",
	"s",
	"d",
	"w",
	"Up",
	"Left",
	"Down",
	"Right",
	"Control",
	"space",
	NULL
};

static int default_pc_map[MAXKEY] =
	{ 46, 44, 30, 31, 32, 17, 103, 105, 108, 106, 29, 57 };

static int default_ppc_map[MAXKEY] =
	{  8,  6,  0,  1,  2, 13,  62,  59,  61,  60, 54, -1 };

static int key_up_vals[MAXKEY] = {
	KEYBOARD_KEY_UP_C,
	KEYBOARD_KEY_UP_Z,
	KEYBOARD_KEY_UP_A,
	KEYBOARD_KEY_UP_S,
	KEYBOARD_KEY_UP_D,
	KEYBOARD_KEY_UP_W,
	KEYBOARD_KEY_UP_ARR_U,
	KEYBOARD_KEY_UP_ARR_L,
	KEYBOARD_KEY_UP_ARR_D,
	KEYBOARD_KEY_UP_ARR_R,
	KEYBOARD_KEY_UP_CTRL,
	KEYBOARD_KEY_UP_SPACE
};


static int key_down_vals[MAXKEY] = {
	KEYBOARD_KEY_DOWN_C,
	KEYBOARD_KEY_DOWN_Z,
	KEYBOARD_KEY_DOWN_A,
	KEYBOARD_KEY_DOWN_S,
	KEYBOARD_KEY_DOWN_D,
	KEYBOARD_KEY_DOWN_W,
	KEYBOARD_KEY_DOWN_ARR_U,
	KEYBOARD_KEY_DOWN_ARR_L,
	KEYBOARD_KEY_DOWN_ARR_D,
	KEYBOARD_KEY_DOWN_ARR_R,
	KEYBOARD_KEY_DOWN_CTRL,
	KEYBOARD_KEY_DOWN_SPACE
};


static int keymap[MAXKEY];


/* Example line of dumpkeys output:
 * "keycode  46 = +c                +C                cent  ..."
 */

int get_config_from_dumpkeys( void )
{
	FILE *fpin;
	char line[MAXLINE];
	char *ptr;
	int codenum;
	int i, ln;

	for( i=0; i<MAXKEY; i++ )
		keymap[i] = 0;

	if( (fpin = popen( "dumpkeys", "r" )) == NULL ) {
		return 0;
	}

	while( fgets(line, MAXLINE, fpin) != NULL ) {
		ptr = line;
		while( *ptr && isspace(*ptr) ) ptr++;		/* skip WS */
		if( *ptr && (*ptr == '!' || *ptr == '#') )
			continue;								/* skip comment lines */
		if( strncasecmp( ptr, "keycode", 7 ) )
			continue;								/* only keycode lines */
		ptr += 7;									/* skip "keycode" */
		while( *ptr && isspace(*ptr) ) ptr++;		/* skip WS */
		if( sscanf( ptr, "%d", &codenum ) != 1 )	/* grab keycode number */
			continue;
		while( *ptr && isdigit(*ptr) ) ptr++;		/* skip number */
		while( *ptr && isspace(*ptr) ) ptr++;		/* skip WS */
		if( *ptr != '=' )
			continue;
		ptr++;										/* skip '=' */
		while( *ptr && isspace(*ptr) ) ptr++;		/* skip WS */
		while( *ptr && *ptr == '+' ) ptr++;			/* skip '+' (optional) */
		for( i=0; keystr[i]; i++ ) {
			ln = strlen(keystr[i]);
			if( !strncasecmp( ptr, keystr[i], ln ) && isspace(*(ptr+ln)) ) {
				if( keymap[i] )						/* not already set */
					continue;
				keymap[i] = codenum;
				break;
			}
		}
	}

	if( ferror(fpin) ) {
		return 0;
	}
	if( pclose(fpin) == -1 ) {
		return 0;
	}

	/* Got all keys? */
	for( i=0; keystr[i]; i++ )
		if( !keymap[i] )
			return 0;

	return 1;
}


void copy_config_from_pc_default( void )
{
	int i;
	for( i=0; keystr[i]; i++ )
		keymap[i] = default_pc_map[i];
}


void copy_config_from_ppc_default( void )
{
	int i;
	for( i=0; keystr[i]; i++ )
		keymap[i] = default_ppc_map[i];
}


void display_config( void )
{
	int i;
	for( i=0; keystr[i]; i++ ) {
		printf( "%12s = %d\n", keystr[i], keymap[i] );
	}
}


int translate_raw_key( int key )
{
	int i;
	for( i=0; keystr[i]; i++ ) {
		if( key == keymap[i] )
			return key_down_vals[i];
		if( key == RELEASE(keymap[i]) )
			return key_up_vals[i];
	}
	return 0;
}


#if 0
int main( void )
{
	if( get_config_from_dumpkeys() )
		printf( "Got config successfully!\n" );

	display_config();
	return 1;
}
#endif