Commit 0483755a4d1fd61fe9c284166f67ae08af8d858b
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
Showing
8 changed files
with
37 additions
and
51 deletions
Makefile
| ... | ... | @@ -137,6 +137,7 @@ endif |
| 137 | 137 | AUDIO_OBJS+= wavcapture.o |
| 138 | 138 | OBJS+=$(addprefix audio/, $(AUDIO_OBJS)) |
| 139 | 139 | |
| 140 | +OBJS+=keymaps.o | |
| 140 | 141 | ifdef CONFIG_SDL |
| 141 | 142 | OBJS+=sdl.o x_keymap.o |
| 142 | 143 | endif |
| ... | ... | @@ -161,15 +162,17 @@ LIBS+=$(VDE_LIBS) |
| 161 | 162 | |
| 162 | 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 | 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 | 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 | 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 | 158 | /* generic keyboard conversion */ |
| 159 | 159 | |
| 160 | 160 | #include "curses_keys.h" |
| 161 | -#include "keymaps.c" | |
| 162 | 161 | |
| 163 | 162 | static kbd_layout_t *kbd_layout = 0; |
| 164 | 163 | static int keycode2keysym[CURSES_KEYS]; |
| ... | ... | @@ -311,7 +310,7 @@ static void curses_keyboard_setup(void) |
| 311 | 310 | keyboard_layout = "en-us"; |
| 312 | 311 | #endif |
| 313 | 312 | if(keyboard_layout) { |
| 314 | - kbd_layout = init_keyboard_layout(keyboard_layout); | |
| 313 | + kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout); | |
| 315 | 314 | if (!kbd_layout) |
| 316 | 315 | exit(1); |
| 317 | 316 | } | ... | ... |
curses_keys.h
| ... | ... | @@ -21,6 +21,10 @@ |
| 21 | 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | 22 | * THE SOFTWARE. |
| 23 | 23 | */ |
| 24 | + | |
| 25 | +#include "keymaps.h" | |
| 26 | + | |
| 27 | + | |
| 24 | 28 | #define KEY_RELEASE 0x80 |
| 25 | 29 | #define KEY_MASK 0x7f |
| 26 | 30 | #define SHIFT_CODE 0x2a |
| ... | ... | @@ -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 | 246 | static const name2keysym_t name2keysym[] = { |
| 248 | 247 | /* Plain ASCII */ |
| 249 | 248 | { "space", 0x020 }, | ... | ... |
keymaps.c
| ... | ... | @@ -22,34 +22,20 @@ |
| 22 | 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 | 31 | const name2keysym_t *p; |
| 28 | - for(p = name2keysym; p->name != NULL; p++) { | |
| 32 | + for(p = table; p->name != NULL; p++) { | |
| 29 | 33 | if (!strcmp(p->name, name)) |
| 30 | 34 | return p->keysym; |
| 31 | 35 | } |
| 32 | 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 | 40 | static void add_to_key_range(struct key_range **krp, int code) { |
| 55 | 41 | struct key_range *kr; |
| ... | ... | @@ -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 | 64 | kbd_layout_t * k) |
| 78 | 65 | { |
| 79 | 66 | FILE *f; |
| ... | ... | @@ -102,7 +89,7 @@ static kbd_layout_t *parse_keyboard_layout(const char *language, |
| 102 | 89 | if (!strncmp(line, "map ", 4)) |
| 103 | 90 | continue; |
| 104 | 91 | if (!strncmp(line, "include ", 8)) { |
| 105 | - parse_keyboard_layout(line + 8, k); | |
| 92 | + parse_keyboard_layout(table, line + 8, k); | |
| 106 | 93 | } else { |
| 107 | 94 | char *end_of_keysym = line; |
| 108 | 95 | while (*end_of_keysym != 0 && *end_of_keysym != ' ') |
| ... | ... | @@ -110,7 +97,7 @@ static kbd_layout_t *parse_keyboard_layout(const char *language, |
| 110 | 97 | if (*end_of_keysym) { |
| 111 | 98 | int keysym; |
| 112 | 99 | *end_of_keysym = 0; |
| 113 | - keysym = get_keysym(line); | |
| 100 | + keysym = get_keysym(table, line); | |
| 114 | 101 | if (keysym == 0) { |
| 115 | 102 | // fprintf(stderr, "Warning: unknown keysym %s\n", line); |
| 116 | 103 | } else { |
| ... | ... | @@ -154,12 +141,14 @@ static kbd_layout_t *parse_keyboard_layout(const char *language, |
| 154 | 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 | 153 | kbd_layout_t *k = kbd_layout; |
| 165 | 154 | if (keysym < MAX_NORMAL_KEYCODE) { |
| ... | ... | @@ -180,7 +169,7 @@ static int keysym2scancode(void *kbd_layout, int keysym) |
| 180 | 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 | 174 | kbd_layout_t *k = kbd_layout; |
| 186 | 175 | struct key_range *kr; |
| ... | ... | @@ -191,7 +180,7 @@ static inline int keycode_is_keypad(void *kbd_layout, int keycode) |
| 191 | 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 | 185 | kbd_layout_t *k = kbd_layout; |
| 197 | 186 | struct key_range *kr; | ... | ... |
sdl.c
| ... | ... | @@ -109,7 +109,6 @@ static void sdl_resize(DisplayState *ds) |
| 109 | 109 | /* generic keyboard conversion */ |
| 110 | 110 | |
| 111 | 111 | #include "sdl_keysym.h" |
| 112 | -#include "keymaps.c" | |
| 113 | 112 | |
| 114 | 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 | 676 | keyboard_layout = "en-us"; |
| 678 | 677 | #endif |
| 679 | 678 | if(keyboard_layout) { |
| 680 | - kbd_layout = init_keyboard_layout(keyboard_layout); | |
| 679 | + kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout); | |
| 681 | 680 | if (!kbd_layout) |
| 682 | 681 | exit(1); |
| 683 | 682 | } | ... | ... |
sdl_keysym.h
vnc.c
| ... | ... | @@ -36,7 +36,6 @@ |
| 36 | 36 | |
| 37 | 37 | #include "vnc.h" |
| 38 | 38 | #include "vnc_keysym.h" |
| 39 | -#include "keymaps.c" | |
| 40 | 39 | #include "d3des.h" |
| 41 | 40 | |
| 42 | 41 | #ifdef CONFIG_VNC_TLS |
| ... | ... | @@ -2422,9 +2421,9 @@ void vnc_display_init(DisplayState *ds) |
| 2422 | 2421 | vs->ds = ds; |
| 2423 | 2422 | |
| 2424 | 2423 | if (keyboard_layout) |
| 2425 | - vs->kbd_layout = init_keyboard_layout(keyboard_layout); | |
| 2424 | + vs->kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout); | |
| 2426 | 2425 | else |
| 2427 | - vs->kbd_layout = init_keyboard_layout("en-us"); | |
| 2426 | + vs->kbd_layout = init_keyboard_layout(name2keysym, "en-us"); | |
| 2428 | 2427 | |
| 2429 | 2428 | if (!vs->kbd_layout) |
| 2430 | 2429 | exit(1); | ... | ... |