Skip to content

Commit

Permalink
Keymaps can now map scancodes into keys
Browse files Browse the repository at this point in the history
Closes #178
  • Loading branch information
kometbomb committed Apr 19, 2015
1 parent 3d80159 commit 57648ed
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
37 changes: 30 additions & 7 deletions src/key.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,14 @@ void translate_key_event(SDL_KeyboardEvent *e)
{
const int allowed = KMOD_SHIFT|KMOD_CTRL|KMOD_ALT;

//debug("key = %u mod = %u %u", e->keysym.sym, e->keysym.mod & allowed, keytrans[0].from_mod);
debug("scancode = %x", e->keysym.scancode);

for (int i = 0 ; i < MAX_KEYTRANS && !(keytrans[i].from_key == 0 && keytrans[i].from_mod == 0) ; ++i)
for (int i = 0 ; i < MAX_KEYTRANS && !(keytrans[i].from_key == 0 && keytrans[i].from_mod == 0 && keytrans[i].from_scancode == 0) ; ++i)
{
if ((keytrans[i].focus == mused.focus || keytrans[i].focus == -1) && e->keysym.sym == keytrans[i].from_key && ((e->keysym.mod & allowed) == keytrans[i].from_mod))
if ((keytrans[i].focus == mused.focus || keytrans[i].focus == -1) &&
((keytrans[i].type == KEYSYM && e->keysym.sym == keytrans[i].from_key) ||
(keytrans[i].type == SCANCODE && e->keysym.scancode == keytrans[i].from_scancode)) &&
((e->keysym.mod & allowed) == keytrans[i].from_mod))
{
e->keysym.sym = keytrans[i].to_key;
e->keysym.mod = keytrans[i].to_mod;
Expand Down Expand Up @@ -133,11 +136,14 @@ void update_keymap_menu()
}


int parse_key(const char *keys, int *key, int *mod)
int parse_key(const char *keys, int *key, int *mod, int *scancode)
{
*mod = 0;
*key = 0;

if (scancode)
*scancode = 0;

char *temp = strdup(keys);
int done = 0;
char *tok = strtok(temp, " \t");
Expand Down Expand Up @@ -173,6 +179,18 @@ int parse_key(const char *keys, int *key, int *mod)
}
}

if (scancode)
{
int _scancode = 0;

if (sscanf(tok, "S_%x", &_scancode) == 1)
{
debug("Found scancode %x", _scancode);
*scancode = _scancode;
found = 1;
}
}

if (!found && strlen(tok) > 0)
{
warning("Unknown token %s", tok);
Expand All @@ -191,15 +209,20 @@ int parse_key(const char *keys, int *key, int *mod)
*mod = 0;
}

return (*key != 0);
return (*key != 0 || (scancode == NULL || *scancode != 0));
}


int parse_keys(const char *from, const char *to, KeyTran *tran)
{
if (!parse_key(from, &tran->from_key, &tran->from_mod)) return 0;
if (!parse_key(from, &tran->from_key, &tran->from_mod, &tran->from_scancode)) return 0;

if (!parse_key(to, &tran->to_key, &tran->to_mod)) return 0;
if (!parse_key(to, &tran->to_key, &tran->to_mod, NULL)) return 0;

if (tran->from_scancode)
tran->type = SCANCODE;
else
tran->type = KEYSYM;

return 1;
}
Expand Down
5 changes: 5 additions & 0 deletions src/keytab.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,18 @@ OTHER DEALINGS IN THE SOFTWARE.

#include "SDL.h"

typedef enum { KEYSYM, SCANCODE } KeyTranType;

typedef struct
{
KeyTranType type;
int focus;
int from_mod;
int from_key;
int from_scancode;
int to_mod;
int to_key;
int to_scancode;
} KeyTran;

typedef struct
Expand Down

0 comments on commit 57648ed

Please sign in to comment.