-
Notifications
You must be signed in to change notification settings - Fork 14
/
keymap.c
180 lines (156 loc) · 4.32 KB
/
keymap.c
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
/*
* Copyright (c) 2012 Citrix Systems, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <ctype.h>
#include "project.h"
#define NUM_MODIFIER_COLS 2
static int fd = -1;
static int modifiers[][NUM_MODIFIER_COLS] =
{
{0, 0},
{KEY_RIGHTSHIFT, KEY_LEFTSHIFT},
{KEY_RIGHTALT, 0},
{0, 0},
{KEY_RIGHTCTRL, KEY_LEFTCTRL},
{0, 0},
{0, 0},
{0, 0},
{KEY_LEFTALT, 0},
};
static int get_bind(u_char index, u_char table)
{
struct kbentry ke;
ke.kb_index = index;
ke.kb_table = table;
if (ioctl(fd, KDGKBENT, (unsigned long)&ke))
return 0;
return ke.kb_value & 0xff;
}
int keycode2ascii(int keycode)
{
int i, j;
int table = 0;
int value;
if (fd == -1)
fd = open("/dev/tty0", O_RDWR);
for (i = 0; i <= 8; i++)
for (j = 0; (j < NUM_MODIFIER_COLS) && (modifiers[i][j]); j++)
if (key_status_get(modifiers[i][j]))
table |= i;
value = get_bind(keycode, table);
return (isprint(value) || (value >= 128 && value <= 255)) ? value : 0;
}
/** Return current keymap from the config file (free the returned string yourself), or NULL */
char *get_configured_keymap( void )
{
char *rval = NULL;
FILE *f = NULL;
char line[256] = { 0 };
f = fopen(KEYMAP_CONF_FILE, "r");
if (!f) goto error;
if (fgets(line, sizeof(line), f) == NULL) goto error;
char *text = strstr(line, "KEYBOARD='");
if (!text) goto error;
char *quoteend = strstr(text + strlen("KEYBOARD='"), "'");
if (!quoteend) goto error;
unsigned long beg = (unsigned long)text + strlen("KEYBOARD='");
unsigned long len = (unsigned long)quoteend - beg;
char layout[32] = { 0 };
strncpy( layout, (char*)beg, MIN(len, sizeof(layout)) );
rval = strdup( layout );
goto success;
error:
rval = NULL;
success:
if (f) {
fclose(f);
}
return rval;
}
/**
* For the moment only fd leak hunt is opened, the moment leak hunt is not
* opened.
* */
static void leak_hunt(void)
{
int i;
int maxfd = sysconf(_SC_OPEN_MAX);
for (i = 3; i < maxfd; ++i) {
close(i);
}
}
/** Change current keymap */
int loadkeys(const char *keymap)
{
pid_t pid;
const char *bin = "/usr/bin/loadkeys";
int status;
/* invoke loadkeys */
info("going to call %s with %s", bin, keymap);
pid = fork();
if (pid == 0) { /* child */
leak_hunt();
execl(bin, bin, keymap, NULL);
error("execl failed for %s with error %s", bin, strerror (errno));
exit (1);
}
else if (pid == -1) {
error("failed to fork: %s", strerror (errno));
return -1;
}
waitpid(pid, &status, 0);
if (!WIFEXITED(status)) {
return -1;
}
int exitstatus = WEXITSTATUS(status);
if (exitstatus!=0)
return exitstatus;
#if 0
/* Silly fork to check that fd was close */
pid = fork();
if (pid == 0) { /* child */
leak_hunt();
while (1);
exit(1);
}
#endif
/* write keymap to conf file */
FILE *f = fopen(KEYMAP_CONF_FILE, "w");
if (f) {
fprintf(f, "KEYBOARD='%s'\n", keymap);
fflush( f );
fsync( fileno(f) );
fclose( f );
}
/* write keymap to xenstore */
xenstore_write( keymap, "/xenclient/keyboard/layout" );
/* permission misery */
char perm[16] = { 0 };
snprintf(perm, sizeof(perm), "r0");
xenstore_chmod ( perm, 1, "/xenclient/keyboard/layout" );
/* success */
return 0;
}
void keymap_init( )
{
char *keys = get_configured_keymap( );
if (keys) {
info("configuring initial keymap: %s", keys);
loadkeys( keys );
free( keys );
}
}