From 60a929a250cf4f7f87ac082e5705f9a838a7f8c8 Mon Sep 17 00:00:00 2001 From: Franklin Wei Date: Tue, 17 Apr 2018 16:18:16 -0400 Subject: Add a request_keys() function with a midend wrapper. This function gives the front end a way to find out what keys the back end requires; and as such it is mostly useful for ports without a keyboard. It is based on changes originally found in Chris Boyle's Android port, though some modifications were needed to make it more flexible. --- misc.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'misc.c') diff --git a/misc.c b/misc.c index 852f869..917958a 100644 --- a/misc.c +++ b/misc.c @@ -21,6 +21,15 @@ void free_cfg(config_item *cfg) sfree(cfg); } +void free_keys(key_label *keys, int nkeys) +{ + int i; + + for(i = 0; i < nkeys; i++) + sfree(keys->label); + sfree(keys); +} + /* * The Mines (among others) game descriptions contain the location of every * mine, and can therefore be used to cheat. @@ -393,4 +402,40 @@ void copy_left_justified(char *buf, size_t sz, const char *str) buf[sz - 1] = 0; } +/* Returns a dynamically allocated label for a generic button. + * Game-specific buttons should go into the `label' field of key_label + * instead. */ +char *button2label(int button) +{ + /* check if it's a keyboard button */ + if(('A' <= button && button <= 'Z') || + ('a' <= button && button <= 'z') || + ('0' <= button && button <= '9') ) + { + char str[2] = { button, '\0' }; + return dupstr(str); + } + + switch(button) + { + case CURSOR_UP: + return dupstr("Up"); + case CURSOR_DOWN: + return dupstr("Down"); + case CURSOR_LEFT: + return dupstr("Left"); + case CURSOR_RIGHT: + return dupstr("Right"); + case CURSOR_SELECT: + return dupstr("Select"); + case '\b': + return dupstr("Clear"); + default: + fatal("unknown generic key"); + } + + /* should never get here */ + return NULL; +} + /* vim: set shiftwidth=4 tabstop=8: */ -- cgit v1.1