preprations for more flexible key handling in menus

This commit is contained in:
marschap
2007-10-04 21:00:20 +00:00
parent 949d74d8e0
commit 254bd128ef
5 changed files with 135 additions and 89 deletions
+71 -41
View File
@@ -48,6 +48,7 @@ char *up_key;
char *down_key;
char *left_key;
char *right_key;
static int keymask; /* mask of defined menu keys */
Screen *menuscreen = NULL;
MenuItem *active_menuitem = NULL;
@@ -78,27 +79,55 @@ int menuscreens_init(void)
debug(RPT_DEBUG, "%s()", __FUNCTION__);
/* Get keys from config file */
menu_key = strdup(config_get_string("menu", "MenuKey", 0, "Menu"));
enter_key = strdup(config_get_string("menu", "EnterKey", 0, "Enter"));
up_key = strdup(config_get_string("menu", "UpKey", 0, "Up"));
down_key = strdup(config_get_string("menu", "DownKey", 0, "Down"));
/* Get keys from config file: MenuKey, EnterKey, UpKey, DownKey, LeftKey, RightKey.
* For a working menu at least 3 are necessary: MenuKey, EnterKey, UpKey/DownKey.
*/
keymask = 0;
menu_key = enter_key = NULL;
tmp = config_get_string("menu", "MenuKey", 0, NULL);
if (tmp != NULL) {
menu_key = strdup(tmp);
keymask |= MENUTOKEN_MENU;
}
tmp = config_get_string("menu", "EnterKey", 0, NULL);
if (tmp != NULL) {
enter_key = strdup(tmp);
keymask |= MENUTOKEN_ENTER;
}
up_key = down_key = NULL;
tmp = config_get_string("menu", "UpKey", 0, NULL);
if (tmp != NULL) {
up_key = strdup(tmp);
keymask |= MENUTOKEN_UP;
}
tmp = config_get_string("menu", "DownKey", 0, NULL);
if (tmp != NULL) {
down_key = strdup(tmp);
keymask |= MENUTOKEN_DOWN;
}
/* if the user has specified in the conf file a left and right key */
left_key = right_key = NULL;
tmp = config_get_string("menu", "LeftKey", 0, NULL);
if (tmp != NULL)
if (tmp != NULL) {
left_key = strdup(tmp);
keymask |= MENUTOKEN_LEFT;
}
tmp = config_get_string("menu", "RightKey", 0, NULL);
if (tmp != NULL)
if (tmp != NULL) {
right_key = strdup(tmp);
keymask |= MENUTOKEN_RIGHT;
}
/* Now reserve keys */
input_reserve_key(menu_key, true, NULL);
input_reserve_key(enter_key, false, NULL);
input_reserve_key(up_key, false, NULL);
input_reserve_key(down_key, false, NULL);
/* Now reserve the keys that were defined */
if (menu_key != NULL)
input_reserve_key(menu_key, true, NULL);
if (enter_key != NULL)
input_reserve_key(enter_key, false, NULL);
if (up_key != NULL)
input_reserve_key(up_key, false, NULL);
if (down_key != NULL)
input_reserve_key(down_key, false, NULL);
if (left_key != NULL)
input_reserve_key(left_key, false, NULL);
if (right_key != NULL)
@@ -144,14 +173,19 @@ int menuscreens_shutdown(void)
/* Forget menu's key reservations */
input_release_client_keys(NULL);
free(menu_key);
free(enter_key);
free(up_key);
free(down_key);
if (menu_key != NULL)
free(menu_key);
if (enter_key != NULL)
free(enter_key);
if (up_key != NULL)
free(up_key);
if (down_key != NULL)
free(down_key);
if (left_key != NULL)
free(left_key);
if (right_key != NULL)
free(right_key);
keymask = 0;
return 0;
}
@@ -188,7 +222,7 @@ void menuscreen_inform_item_modified(MenuItem *item)
bool is_menu_key(const char *key)
{
if (menu_key && key && strcmp(key, menu_key) == 0)
if ((menu_key != NULL) && (key != NULL) && (strcmp(key, menu_key) == 0))
return true;
else
return false;
@@ -284,10 +318,9 @@ static void handle_predecessor(void)
assert(item != NULL);
debug(RPT_DEBUG, "%s: Switching to registered predecessor '%s' of '%s'.",
__FUNCTION__, item->predecessor_id, item->id);
predecessor = menuitem_search(
item->predecessor_id, (Client*)active_menuitem->client);
if (predecessor == NULL)
{
predecessor = menuitem_search(item->predecessor_id,
(Client *) active_menuitem->client);
if (predecessor == NULL) {
// note: if _quit_, _close_, _none_ get here this
// would be an implementation error - they should
// have been handled via different MENURESULT codes.
@@ -306,9 +339,8 @@ static void handle_predecessor(void)
menuitem_update_screen(active_menuitem, menuscreen);
break;
default:
if (predecessor->parent != NULL
&& predecessor->parent->type == MENUITEM_MENU)
{
if ((predecessor->parent != NULL) &&
(predecessor->parent->type == MENUITEM_MENU)) {
// update parent menu too
menu_select_subitem(predecessor->parent, predecessor->id);
}
@@ -326,10 +358,9 @@ static void handle_successor(void)
assert(item != NULL);
debug(RPT_DEBUG, "%s: Switching to registered successor '%s' of '%s'.",
__FUNCTION__, item->successor_id, item->id);
successor = menuitem_search(
item->successor_id, (Client*)active_menuitem->client);
if (successor == NULL)
{
successor = menuitem_search(item->successor_id,
(Client *) active_menuitem->client);
if (successor == NULL) {
// note: if _quit_, _close_, _none_ get here this
// would be an implementation error - they should
// have been handled via different MENURESULT codes.
@@ -348,9 +379,8 @@ static void handle_successor(void)
menuitem_update_screen(active_menuitem, menuscreen);
break;
default:
if (successor->parent != NULL
&& successor->parent->type == MENUITEM_MENU)
{
if ((successor->parent != NULL) &&
(successor->parent->type == MENUITEM_MENU)) {
// update parent menu too
menu_select_subitem(successor->parent, successor->id);
}
@@ -361,27 +391,27 @@ static void handle_successor(void)
void menuscreen_key_handler(const char *key)
{
char token = 0;
MenuToken token = MENUTOKEN_NONE;
MenuResult res;
debug(RPT_DEBUG, "%s(\"%s\")", __FUNCTION__, key);
if (strcmp(key, menu_key) == 0) {
if ((menu_key != NULL) && (strcmp(key, menu_key) == 0)) {
token = MENUTOKEN_MENU;
}
else if (strcmp(key, enter_key) == 0) {
else if ((enter_key != NULL) && (strcmp(key, enter_key) == 0)) {
token = MENUTOKEN_ENTER;
}
else if (strcmp(key, up_key) == 0) {
else if ((up_key != NULL) && (strcmp(key, up_key) == 0)) {
token = MENUTOKEN_UP;
}
else if (strcmp(key, down_key) == 0) {
else if ((down_key != NULL) && (strcmp(key, down_key) == 0)) {
token = MENUTOKEN_DOWN;
}
else if (left_key && strcmp(key, left_key) == 0) {
else if ((left_key != NULL) && (strcmp(key, left_key) == 0)) {
token = MENUTOKEN_LEFT;
}
else if (right_key && strcmp(key, right_key) == 0) {
else if ((right_key != NULL) && (strcmp(key, right_key) == 0)) {
token = MENUTOKEN_RIGHT;
}
else {
@@ -396,7 +426,7 @@ void menuscreen_key_handler(const char *key)
}
res = menuitem_process_input(active_menuitem, token, key,
((left_key || right_key) ? 1 : 0));
(keymask & (MENUTOKEN_LEFT | MENUTOKEN_RIGHT)) ? 1 : 0);
switch (res) {
case MENURESULT_ERROR: