action.c keyremap clean-up add logf to core_keymap.c

move the remap out of the loop and eliminate a status flag

Change-Id: I9ab841037a8ea7dff736b452ff55e8242084af82
This commit is contained in:
William Wilgus 2022-02-25 23:50:38 -05:00
parent 887249671c
commit 6b360d8b04
3 changed files with 59 additions and 46 deletions

View file

@ -70,7 +70,6 @@ static action_last_t action_last =
.wait_for_release = false,
#ifndef DISABLE_ACTION_REMAP
.check_remap = false,
.core_keymap = NULL,
#endif
@ -590,12 +589,8 @@ static inline int get_next_context(const struct button_mapping *items, int i)
*/
static inline void action_code_lookup(action_last_t *last, action_cur_t *cur)
{
int action = ACTION_NONE;
int action, i;
int context = cur->context;
int i = 0;
#ifndef DISABLE_ACTION_REMAP
last->check_remap = (last->core_keymap != NULL);
#endif
cur->is_prebutton = false;
#ifdef HAVE_LOCKED_ACTIONS
@ -605,6 +600,41 @@ static inline void action_code_lookup(action_last_t *last, action_cur_t *cur)
context |= CONTEXT_LOCKED;
#endif
#ifndef DISABLE_ACTION_REMAP
bool check_remap = (last->core_keymap != NULL);
/* attempt to look up the button in user supplied remap */
if(check_remap && (context & CONTEXT_PLUGIN) == 0)
{
#if 0 /*Disable the REMOTE context for remap for now (BUTTON_REMOTE != 0)*/
if ((cur->button & BUTTON_REMOTE) != 0)
{
context |= CONTEXT_REMOTE;
}
#endif
cur->items = last->core_keymap;
i = 0;
action = ACTION_UNKNOWN;
/* check the lut at the beginning for the desired context */
while (cur->items[i].button_code != BUTTON_NONE)
{
if (cur->items[i].action_code == CORE_CONTEXT_REMAP(context))
{
i = cur->items[i].button_code;
action = action_code_worker(last, cur, &i);
if (action != ACTION_UNKNOWN)
{
cur->action = action;
return;
}
}
i++;
}
}
#endif
i = 0;
action = ACTION_NONE;
/* attempt to look up the button in the in-built keymaps */
for(;;)
{
/* logf("context = %x",context); */
@ -619,35 +649,6 @@ static inline void action_code_lookup(action_last_t *last, action_cur_t *cur)
{
cur->items = cur->get_context_map(context);
}
#ifndef DISABLE_ACTION_REMAP
else if(last->check_remap) /* attempt to look up the button in user supplied remap */
{
cur->items = last->core_keymap;
i = 0;
action = ACTION_UNKNOWN;
/* check the lut at the beginning for the desired context */
while (cur->items[i].action_code != (int) CONTEXT_STOPSEARCHING)
{
if (cur->items[i].action_code == CORE_CONTEXT_REMAP(context))
{
i = cur->items[i].button_code;
action = action_code_worker(last, cur, &i);
break;
}
i++;
}
if (action != ACTION_UNKNOWN)
break;
else
{
/* Not found -- fall through to inbuilt keymaps */
i = 0;
last->check_remap = false;
cur->items = get_context_mapping(context);
}
}
#endif
else
{
cur->items = get_context_mapping(context);
@ -1199,17 +1200,21 @@ int action_set_keymap(struct button_mapping* core_keymap, int count)
if (count > 0 && core_keymap != NULL) /* saf-tey checks :) */
{
int i = 0;
if (core_keymap[count - 1].action_code != (int) CONTEXT_STOPSEARCHING ||
core_keymap[count - 1].button_code != BUTTON_NONE) /* check for sentinel at end*/
struct button_mapping* entry = &core_keymap[count - 1];
if (entry->action_code != (int) CONTEXT_STOPSEARCHING ||
entry->button_code != BUTTON_NONE) /* check for sentinel at end*/
{
count = -1;
}
/* check the lut at the beginning for invalid offsets */
while (count > 0 && core_keymap[i].action_code != (int) CONTEXT_STOPSEARCHING)
while (count > 0 && /* check the lut at the beginning for invalid offsets */
(entry = &core_keymap[i])->action_code != (int) CONTEXT_STOPSEARCHING)
{
if ((core_keymap[i].action_code & CONTEXT_REMAPPED) == CONTEXT_REMAPPED)
if ((entry->action_code & CONTEXT_REMAPPED) == CONTEXT_REMAPPED)
{
int firstbtn = core_keymap[i].button_code;
int endpos = firstbtn + core_keymap[i].pre_button_code;
int firstbtn = entry->button_code;
int endpos = firstbtn + entry->pre_button_code;
if (firstbtn > count || firstbtn < i || endpos > count)
{
/* offset out of bounds */
@ -1217,7 +1222,7 @@ int action_set_keymap(struct button_mapping* core_keymap, int count)
break;
}
if (core_keymap[endpos].action_code != (int) CONTEXT_STOPSEARCHING)
if (core_keymap[endpos].button_code != BUTTON_NONE)
{
/* stop sentinel is not at end of action lut*/
count = -3;

View file

@ -419,7 +419,6 @@ typedef struct
bool wait_for_release;
#ifndef DISABLE_ACTION_REMAP
bool check_remap;
struct button_mapping* core_keymap;
#endif

View file

@ -23,6 +23,9 @@
#include "core_alloc.h"
#include "core_keymap.h"
/*#define LOGF_ENABLE*/
#include "logf.h"
#if !defined(__PCTOOL__) || defined(CHECKWPS)
static int keymap_handle = -1;
@ -76,6 +79,7 @@ int core_load_key_remap(const char *filename)
if (core_alloc_keymap(fsize) <= 0)
{
count = -30;
logf("core_keymap: %d Failed to allocate buffer", count);
break;
}
buf = core_get_data(keymap_handle);
@ -84,7 +88,10 @@ int core_load_key_remap(const char *filename)
count = action_set_keymap((struct button_mapping *) buf, count);
}
else
{
count = -40;
logf("core_keymap: %d Failed to read", count);
}
break;
}
close(fd);
@ -108,6 +115,7 @@ int open_key_remap(const char *filename, int *fd, size_t *fsize)
if (count * sizeof(struct button_mapping) != *fsize)
{
count = -10;
logf("core_keymap: %d Size mismatch", count);
break;
}
@ -125,6 +133,7 @@ int open_key_remap(const char *filename, int *fd, size_t *fsize)
else /* Header mismatch */
{
count = -20;
logf("core_keymap: %d Header mismatch", count);
break;
}
}