File: keycodes.c

package info (click to toggle)
mol 0.9.61-6
  • links: PTS
  • area: contrib
  • in suites: woody
  • size: 6,140 kB
  • ctags: 8,491
  • sloc: ansic: 50,560; asm: 2,826; sh: 458; makefile: 373; perl: 165; lex: 135; yacc: 131
file content (242 lines) | stat: -rw-r--r-- 6,019 bytes parent folder | download
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
/* 
 *   Creation Date: <2000/12/09 16:16:40 samuel>
 *   Time-stamp: <2001/09/30 16:40:32 samuel>
 *   
 *	<keycodes.c>
 *	
 *	Handles different keyboard layouts and user customization
 *   
 *   Copyright (C) 2000, 2001 Samuel Rydh (samuel@ibrium.se)
 *   
 *   Based upon initial work by Ian Reinhart Geiser <geiseri@yahoo.com>
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   as published by the Free Software Foundation
 *   
 */

#include "mol_config.h"

#include <sys/param.h>
#include "keycodes.h"
#include "debugger.h"
#include "res_manager.h"
#include "driver_mgr.h"
#include "mol_assert.h"

int _uses_linux_keycodes;

#define USE_LINUX_KEYCODES_RES		"use_linux_keycodes"
#define SHOW_KEY_TRANS_RES		"show_key_trans"

static char *key_res_names[ NUM_KEYCODE_TABLES ] = {
	"remap_key",
	"remap_xkey"
};

static char *file_res_names[ NUM_KEYCODE_TABLES ] = {
	"kbd_file",
	"xkbd_file"
};

/* 108 is the "eject CD" button */

static const unsigned char linux_to_adb_keycodes[128] = {
/* 0 */	 -1,  53,  18,  19,  20,  21,  23,  22,  26,  28,
	 25,  29,  27,  24,  51,  48,  12,  13,  14,  15,
/*20*/	 17,  16,  32,  34,  31,  35,  33,  30,  36,  54,
 	  0,   1,   2,   3,   5,   4,  38,  40,  37,  41,
/*40*/	 39,  10,  56,  42,   6,   7,   8,   9,  11,  45,
	 46,  43,  47,  44, 123,  67,  58,  49,  57, 122,
/*60*/  120,  99, 118,  96,  97,  98, 100, 101, 109,  71,
	107,  89,  91,  92,  78,  86,  87,  88,  69,  83,
/*80*/	 84,  85,  82,  65,  -1, 105,  50, 103, 111,  -1,
	 -1,  -1,  -1,  -1,  -1,  -1,  76,  54,  75, 105,
/*100*/	 58,  -1, 115,  62, 116,  59,  60, 119,  61, 121,
	114, 117,  -1,  -1,  -1, 107,  -1,  81,  -1, 113,
/*120*/	 -1,  -1,  -1,  -1,  -1,  55,  55,  -1
};

typedef struct {
	char	*table;
	int 	max;		/* num keycodes in table */
	int	min;		/* lowest keycode in use */
} keytable_t;

static keytable_t ktab[ NUM_KEYCODE_TABLES ];

static int initialized;
static int verbose_key_trans;

static void 	parse_kbd_files( int ktype, char *res_name );
static void 	parse_kbd_resources( int ktype, char *res_name );
extern int 	keycodes_init( void );
extern void 	keycodes_cleanup( void );

driver_interface_t keycodes_driver = {
    "keycodes", keycodes_init, keycodes_cleanup
};


int
keycodes_init( void )
{
	FILE *f;
	
	_uses_linux_keycodes = 1;
	verbose_key_trans = get_bool_res( SHOW_KEY_TRANS_RES ) == 1;
	
	if( !get_bool_res( USE_LINUX_KEYCODES_RES ) ) {
		_uses_linux_keycodes = 0;
	} else if( (f = fopen("/proc/sys/dev/mac_hid/keyboard_sends_linux_keycodes", "r")) ) {
		if( fgetc(f) == '0' )
			_uses_linux_keycodes = 0;
		fclose(f);
	} else {
		printm("Could not open /proc/sys/dev/mac_hid/keyboard_sends_linux_keycodes\n");
	}
	printm( "Using %s keycodes\n", _uses_linux_keycodes ? "Linux" : "ADB");
	initialized = 1;
	return 1;
}

void
keycodes_cleanup( void )
{
	int i;
	for(i=0; i<NUM_KEYCODE_TABLES; i++ )
		if( ktab[i].table )
			free( ktab[i].table );
}

int
register_key_table( int ktype, int min, int max )
{
	keytable_t *kt = &ktab[ktype];
	char *p;
	int i, size;
	
	assert( initialized && !kt->table && min <= max );

	size = max-min+1;
	p = kt->table = malloc( size );
	kt->max = max;
	kt->min = min;

	memset( p, 0xff, size );
	for( i=0; i<size; i++, p++ ) {
		if( _uses_linux_keycodes && i<sizeof(linux_to_adb_keycodes) )
			*p = linux_to_adb_keycodes[i];
		if( !_uses_linux_keycodes && i<128 )
			*p = i;
	}
	return 0;
}

int 
user_kbd_customize( int ktype ) 
{
	parse_kbd_files( ktype, file_res_names[ktype] );
	parse_kbd_resources( ktype, key_res_names[ktype] );
	return 0;
}


/************************************************************************/
/*	F U N C T I O N S						*/
/************************************************************************/

int
keycode_to_adb( int ktype, int keycode, int is_keypress )
{
	keytable_t *kt = &ktab[ktype];
	int ret;

	assert(kt->table);
	if( keycode > kt->max || keycode < kt->min ) {
		printm("Keycode %d out of range\n", keycode );
		keycode = kt->min;
	}

	ret = kt->table[ keycode - kt->min ];
	if( ret == 0xff ){
		printm("Keycode %d has no corresponding ADB keycode (table %d)\n", keycode, ktype );
		return 0x43 | (is_keypress? 0 : 0x80);
	}
	if( verbose_key_trans && is_keypress )
		printm("keycode %3d (0x%02x) --> ADB-code %3d (0x%02x)\n", keycode,keycode,ret,ret);

	return (ret & 0x7f) | (is_keypress? 0 : 0x80);
}

void
set_keycode( int ktype, int keycode, int adbcode )
{
	keytable_t *kt = &ktab[ktype];

	assert(kt->table);

	if( keycode > kt->max || keycode < kt->min ) {
		printm("keycode %d (table %d) out of range (%d...%d)\n", keycode, ktype, kt->min, kt->max );
		return;
	}
	if( adbcode >= 128 || adbcode < 0 ) {
		printm("Adbcode %d out of range (max=127)\n", adbcode);
		return;
	}
	kt->table[keycode-kt->min] = adbcode;
}

static void
parse_kbd_files( int ktype, char *res_name )
{
	char *name;
	unsigned int a,b;
        int n, r, ind=0;
	FILE *f;

	while( (name=get_str_res_ind( res_name, ind++, 0 )) ) {
		if( !(f = fopen( name, "r" ) )) {
			printm("Could not open '%s'\n", name );
			continue;
		}
		n=0;
		while( (r=fscanf(f, " %d : %d ", &a, &b )) != EOF) {
			if( r != 2 )
				r=fscanf(f, " 0x%x : 0x%x ", &a, &b );
			if( r != 2 ) {
				printm("Parse error in '%s' (%d pairs read)\n", name, n );
				break;
			}
			n++;
			set_keycode( ktype, a, b );
			/* printm("KEYREMAP: %d --> %d\n", a, b ); */
		}
		fclose(f);
	}
}

static void
parse_kbd_resources( int ktype, char *res_name )
{
	unsigned int a,b, ind=0;
	char *s1,*s2;
	int err;

	while( (s1=get_str_res_ind( res_name, ind, 0 )) ) {
		err = !(s2=get_str_res_ind( res_name, ind++, 1 ));

		if( err || !(sscanf(s1,"0x%x", &a) || sscanf(s1,"%d",&a)) 
		    || !(sscanf(s2,"0x%x",&b) || sscanf(s2,"%d", &b)) )
			err++;

		if( err ) {
			printm("Bad keyremap resource.\n");
			printm("Correct syntax: 'keyremap: old_keycode new_keycode'\n");
		} else {
			set_keycode( ktype, a, b );
			printm("KEYREMAP: %d --> %d\n", a, b );
		}
	}
}