Commit 0483755a4d1fd61fe9c284166f67ae08af8d858b

Authored by aliguori
1 parent 1ff7df1a

Refactor keymap code to avoid duplication ("Daniel P. Berrange")

Each of the graphical frontends #include a .c file, for keymap code
resulting in duplicated definitions & duplicated compiled code. A
couple of small changes allowed this to be sanitized, so instead of
doing a #include "keymaps.c", duplicating all code, we can have a
shared keymaps.h file, and only compile code once. This allows the
next patch to move the VncState struct out into a header file without
causing clashing definitions.


 Makefile      |    9 +++++---
 b/keymaps.h   |   60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 curses.c      |    3 --
 curses_keys.h |    9 +++-----
 keymaps.c     |   45 ++++++++++++++++---------------------------
 sdl.c         |    3 --
 sdl_keysym.h  |    7 ++----
 vnc.c         |    5 +---
 vnc_keysym.h  |    7 ++----
 9 files changed, 97 insertions(+), 51 deletions(-)

   Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>


git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@6721 c046a42c-6fe2-441c-8c8c-71466251a162
Makefile
@@ -137,6 +137,7 @@ endif @@ -137,6 +137,7 @@ endif
137 AUDIO_OBJS+= wavcapture.o 137 AUDIO_OBJS+= wavcapture.o
138 OBJS+=$(addprefix audio/, $(AUDIO_OBJS)) 138 OBJS+=$(addprefix audio/, $(AUDIO_OBJS))
139 139
  140 +OBJS+=keymaps.o
140 ifdef CONFIG_SDL 141 ifdef CONFIG_SDL
141 OBJS+=sdl.o x_keymap.o 142 OBJS+=sdl.o x_keymap.o
142 endif 143 endif
@@ -161,15 +162,17 @@ LIBS+=$(VDE_LIBS) @@ -161,15 +162,17 @@ LIBS+=$(VDE_LIBS)
161 162
162 cocoa.o: cocoa.m 163 cocoa.o: cocoa.m
163 164
164 -sdl.o: sdl.c keymaps.c sdl_keysym.h 165 +keymaps.o: keymaps.c keymaps.h
  166 +
  167 +sdl.o: sdl.c keymaps.h sdl_keysym.h
165 168
166 sdl.o audio/sdlaudio.o: CFLAGS += $(SDL_CFLAGS) 169 sdl.o audio/sdlaudio.o: CFLAGS += $(SDL_CFLAGS)
167 170
168 -vnc.o: vnc.c keymaps.c sdl_keysym.h vnchextile.h d3des.c d3des.h 171 +vnc.o: vnc.c keymaps.h sdl_keysym.h vnchextile.h d3des.c d3des.h
169 172
170 vnc.o: CFLAGS += $(CONFIG_VNC_TLS_CFLAGS) 173 vnc.o: CFLAGS += $(CONFIG_VNC_TLS_CFLAGS)
171 174
172 -curses.o: curses.c keymaps.c curses_keys.h 175 +curses.o: curses.c keymaps.h curses_keys.h
173 176
174 bt-host.o: CFLAGS += $(CONFIG_BLUEZ_CFLAGS) 177 bt-host.o: CFLAGS += $(CONFIG_BLUEZ_CFLAGS)
175 178
curses.c
@@ -158,7 +158,6 @@ static void curses_cursor_position(DisplayState *ds, int x, int y) @@ -158,7 +158,6 @@ static void curses_cursor_position(DisplayState *ds, int x, int y)
158 /* generic keyboard conversion */ 158 /* generic keyboard conversion */
159 159
160 #include "curses_keys.h" 160 #include "curses_keys.h"
161 -#include "keymaps.c"  
162 161
163 static kbd_layout_t *kbd_layout = 0; 162 static kbd_layout_t *kbd_layout = 0;
164 static int keycode2keysym[CURSES_KEYS]; 163 static int keycode2keysym[CURSES_KEYS];
@@ -311,7 +310,7 @@ static void curses_keyboard_setup(void) @@ -311,7 +310,7 @@ static void curses_keyboard_setup(void)
311 keyboard_layout = "en-us"; 310 keyboard_layout = "en-us";
312 #endif 311 #endif
313 if(keyboard_layout) { 312 if(keyboard_layout) {
314 - kbd_layout = init_keyboard_layout(keyboard_layout); 313 + kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
315 if (!kbd_layout) 314 if (!kbd_layout)
316 exit(1); 315 exit(1);
317 } 316 }
curses_keys.h
@@ -21,6 +21,10 @@ @@ -21,6 +21,10 @@
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE. 22 * THE SOFTWARE.
23 */ 23 */
  24 +
  25 +#include "keymaps.h"
  26 +
  27 +
24 #define KEY_RELEASE 0x80 28 #define KEY_RELEASE 0x80
25 #define KEY_MASK 0x7f 29 #define KEY_MASK 0x7f
26 #define SHIFT_CODE 0x2a 30 #define SHIFT_CODE 0x2a
@@ -239,11 +243,6 @@ static const int curses2keysym[CURSES_KEYS] = { @@ -239,11 +243,6 @@ static const int curses2keysym[CURSES_KEYS] = {
239 243
240 }; 244 };
241 245
242 -typedef struct {  
243 - const char* name;  
244 - int keysym;  
245 -} name2keysym_t;  
246 -  
247 static const name2keysym_t name2keysym[] = { 246 static const name2keysym_t name2keysym[] = {
248 /* Plain ASCII */ 247 /* Plain ASCII */
249 { "space", 0x020 }, 248 { "space", 0x020 },
keymaps.c
@@ -22,34 +22,20 @@ @@ -22,34 +22,20 @@
22 * THE SOFTWARE. 22 * THE SOFTWARE.
23 */ 23 */
24 24
25 -static int get_keysym(const char *name) 25 +#include "keymaps.h"
  26 +#include "sysemu.h"
  27 +
  28 +static int get_keysym(const name2keysym_t *table,
  29 + const char *name)
26 { 30 {
27 const name2keysym_t *p; 31 const name2keysym_t *p;
28 - for(p = name2keysym; p->name != NULL; p++) { 32 + for(p = table; p->name != NULL; p++) {
29 if (!strcmp(p->name, name)) 33 if (!strcmp(p->name, name))
30 return p->keysym; 34 return p->keysym;
31 } 35 }
32 return 0; 36 return 0;
33 } 37 }
34 38
35 -struct key_range {  
36 - int start;  
37 - int end;  
38 - struct key_range *next;  
39 -};  
40 -  
41 -#define MAX_NORMAL_KEYCODE 512  
42 -#define MAX_EXTRA_COUNT 256  
43 -typedef struct {  
44 - uint16_t keysym2keycode[MAX_NORMAL_KEYCODE];  
45 - struct {  
46 - int keysym;  
47 - uint16_t keycode;  
48 - } keysym2keycode_extra[MAX_EXTRA_COUNT];  
49 - int extra_count;  
50 - struct key_range *keypad_range;  
51 - struct key_range *numlock_range;  
52 -} kbd_layout_t;  
53 39
54 static void add_to_key_range(struct key_range **krp, int code) { 40 static void add_to_key_range(struct key_range **krp, int code) {
55 struct key_range *kr; 41 struct key_range *kr;
@@ -73,7 +59,8 @@ static void add_to_key_range(struct key_range **krp, int code) { @@ -73,7 +59,8 @@ static void add_to_key_range(struct key_range **krp, int code) {
73 } 59 }
74 } 60 }
75 61
76 -static kbd_layout_t *parse_keyboard_layout(const char *language, 62 +static kbd_layout_t *parse_keyboard_layout(const name2keysym_t *table,
  63 + const char *language,
77 kbd_layout_t * k) 64 kbd_layout_t * k)
78 { 65 {
79 FILE *f; 66 FILE *f;
@@ -102,7 +89,7 @@ static kbd_layout_t *parse_keyboard_layout(const char *language, @@ -102,7 +89,7 @@ static kbd_layout_t *parse_keyboard_layout(const char *language,
102 if (!strncmp(line, "map ", 4)) 89 if (!strncmp(line, "map ", 4))
103 continue; 90 continue;
104 if (!strncmp(line, "include ", 8)) { 91 if (!strncmp(line, "include ", 8)) {
105 - parse_keyboard_layout(line + 8, k); 92 + parse_keyboard_layout(table, line + 8, k);
106 } else { 93 } else {
107 char *end_of_keysym = line; 94 char *end_of_keysym = line;
108 while (*end_of_keysym != 0 && *end_of_keysym != ' ') 95 while (*end_of_keysym != 0 && *end_of_keysym != ' ')
@@ -110,7 +97,7 @@ static kbd_layout_t *parse_keyboard_layout(const char *language, @@ -110,7 +97,7 @@ static kbd_layout_t *parse_keyboard_layout(const char *language,
110 if (*end_of_keysym) { 97 if (*end_of_keysym) {
111 int keysym; 98 int keysym;
112 *end_of_keysym = 0; 99 *end_of_keysym = 0;
113 - keysym = get_keysym(line); 100 + keysym = get_keysym(table, line);
114 if (keysym == 0) { 101 if (keysym == 0) {
115 // fprintf(stderr, "Warning: unknown keysym %s\n", line); 102 // fprintf(stderr, "Warning: unknown keysym %s\n", line);
116 } else { 103 } else {
@@ -154,12 +141,14 @@ static kbd_layout_t *parse_keyboard_layout(const char *language, @@ -154,12 +141,14 @@ static kbd_layout_t *parse_keyboard_layout(const char *language,
154 return k; 141 return k;
155 } 142 }
156 143
157 -static void *init_keyboard_layout(const char *language) 144 +
  145 +void *init_keyboard_layout(const name2keysym_t *table, const char *language)
158 { 146 {
159 - return parse_keyboard_layout(language, 0); 147 + return parse_keyboard_layout(table, language, 0);
160 } 148 }
161 149
162 -static int keysym2scancode(void *kbd_layout, int keysym) 150 +
  151 +int keysym2scancode(void *kbd_layout, int keysym)
163 { 152 {
164 kbd_layout_t *k = kbd_layout; 153 kbd_layout_t *k = kbd_layout;
165 if (keysym < MAX_NORMAL_KEYCODE) { 154 if (keysym < MAX_NORMAL_KEYCODE) {
@@ -180,7 +169,7 @@ static int keysym2scancode(void *kbd_layout, int keysym) @@ -180,7 +169,7 @@ static int keysym2scancode(void *kbd_layout, int keysym)
180 return 0; 169 return 0;
181 } 170 }
182 171
183 -static inline int keycode_is_keypad(void *kbd_layout, int keycode) 172 +int keycode_is_keypad(void *kbd_layout, int keycode)
184 { 173 {
185 kbd_layout_t *k = kbd_layout; 174 kbd_layout_t *k = kbd_layout;
186 struct key_range *kr; 175 struct key_range *kr;
@@ -191,7 +180,7 @@ static inline int keycode_is_keypad(void *kbd_layout, int keycode) @@ -191,7 +180,7 @@ static inline int keycode_is_keypad(void *kbd_layout, int keycode)
191 return 0; 180 return 0;
192 } 181 }
193 182
194 -static inline int keysym_is_numlock(void *kbd_layout, int keysym) 183 +int keysym_is_numlock(void *kbd_layout, int keysym)
195 { 184 {
196 kbd_layout_t *k = kbd_layout; 185 kbd_layout_t *k = kbd_layout;
197 struct key_range *kr; 186 struct key_range *kr;
@@ -109,7 +109,6 @@ static void sdl_resize(DisplayState *ds) @@ -109,7 +109,6 @@ static void sdl_resize(DisplayState *ds)
109 /* generic keyboard conversion */ 109 /* generic keyboard conversion */
110 110
111 #include "sdl_keysym.h" 111 #include "sdl_keysym.h"
112 -#include "keymaps.c"  
113 112
114 static kbd_layout_t *kbd_layout = NULL; 113 static kbd_layout_t *kbd_layout = NULL;
115 114
@@ -677,7 +676,7 @@ void sdl_display_init(DisplayState *ds, int full_screen, int no_frame) @@ -677,7 +676,7 @@ void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
677 keyboard_layout = "en-us"; 676 keyboard_layout = "en-us";
678 #endif 677 #endif
679 if(keyboard_layout) { 678 if(keyboard_layout) {
680 - kbd_layout = init_keyboard_layout(keyboard_layout); 679 + kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
681 if (!kbd_layout) 680 if (!kbd_layout)
682 exit(1); 681 exit(1);
683 } 682 }
sdl_keysym.h
1 -typedef struct {  
2 - const char* name;  
3 - int keysym;  
4 -} name2keysym_t; 1 +
  2 +#include "keymaps.h"
  3 +
5 static const name2keysym_t name2keysym[]={ 4 static const name2keysym_t name2keysym[]={
6 /* ascii */ 5 /* ascii */
7 { "space", 0x020}, 6 { "space", 0x020},
@@ -36,7 +36,6 @@ @@ -36,7 +36,6 @@
36 36
37 #include "vnc.h" 37 #include "vnc.h"
38 #include "vnc_keysym.h" 38 #include "vnc_keysym.h"
39 -#include "keymaps.c"  
40 #include "d3des.h" 39 #include "d3des.h"
41 40
42 #ifdef CONFIG_VNC_TLS 41 #ifdef CONFIG_VNC_TLS
@@ -2422,9 +2421,9 @@ void vnc_display_init(DisplayState *ds) @@ -2422,9 +2421,9 @@ void vnc_display_init(DisplayState *ds)
2422 vs->ds = ds; 2421 vs->ds = ds;
2423 2422
2424 if (keyboard_layout) 2423 if (keyboard_layout)
2425 - vs->kbd_layout = init_keyboard_layout(keyboard_layout); 2424 + vs->kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
2426 else 2425 else
2427 - vs->kbd_layout = init_keyboard_layout("en-us"); 2426 + vs->kbd_layout = init_keyboard_layout(name2keysym, "en-us");
2428 2427
2429 if (!vs->kbd_layout) 2428 if (!vs->kbd_layout)
2430 exit(1); 2429 exit(1);
vnc_keysym.h
1 -typedef struct {  
2 - const char* name;  
3 - int keysym;  
4 -} name2keysym_t; 1 +
  2 +#include "keymaps.h"
  3 +
5 static const name2keysym_t name2keysym[]={ 4 static const name2keysym_t name2keysym[]={
6 /* ascii */ 5 /* ascii */
7 { "space", 0x020}, 6 { "space", 0x020},