X-Git-Url: https://plomlompom.com/repos/foo.html?a=blobdiff_plain;ds=inline;f=src%2Fclient%2Fkeybindings.c;h=fc371e866501f7caf9777f021cda9aa2a074e1df;hb=6e1a472e34a2ca5a18211a65a59123dc9c12bc88;hp=73fc2c8541a7ea346c2ed8d8b697206e68d470ae;hpb=cda9c65e89a5dfb0c8306cd7ac74427df29c3c21;p=plomrogue
diff --git a/src/client/keybindings.c b/src/client/keybindings.c
index 73fc2c8..fc371e8 100644
--- a/src/client/keybindings.c
+++ b/src/client/keybindings.c
@@ -1,26 +1,26 @@
-/* src/client/keybindings.c */
+/* src/client/keybindings.c
+ *
+ * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
+ * or any later version. For details on its copyright, license, and warranties,
+ * see the file NOTICE in the root directory of the PlomRogue source package.
+ */
#include "keybindings.h"
-#include /* keycode defines, cbreak(), halfdelay(), getch() */
-#include /* FILE, sprintf(), snprintf() */
+#include /* keycode defines, cbreak(), getch(), timeout() */
+#include /* NULL */
#include /* uint8_t, uint16_t, uint32_t */
-#include /* free(), atoi() */
-#include /* strlen(), strchr(), strcmp(), memcpy() */
-#include "../common/readwrite.h" /* try_fopen(), textfile_sizes(), try_fgets(),
- * try_fclose(), try_fclose_unlink_rename(),
- * try_fwrite()
- */
-#include "../common/try_malloc.h" /* for try_malloc() */
+#include /* FILE, sprintf() */
+#include "../common/rexit.h" /* exit_trouble() */
+#include "../common/try_malloc.h" /* try_malloc() */
#include "windows.h" /* draw_all_wins() */
#include "world.h" /* global world */
+struct Command;
-
-/* Return "n"-th keybinding in keybindings chain from "kb_p" on. */
-static struct KeyBinding * get_keyb_of_n(struct KeyBinding * kb_p, uint16_t n);
-
-/* Return number of keybindings in keybindings chain from "kb_p" on. */
-static uint16_t get_n_of_keybs(struct KeyBinding * kb_p);
+/* Return pointer to global keybindings or to keybindings for wingeometry config
+ * (c = "g") or winkeys config (c = "k") or active window's keybindings ("w").
+ */
+static struct KeyBindingDB * char_selected_kb_db(char c);
/* If "keycode_given" equals "keycode_match", copy "keyname_match" to "keyname"
* and return 1; otherwise return 0.
@@ -30,36 +30,24 @@ static uint8_t try_keycode(uint16_t keycode_given, char * keyname,
-static struct KeyBinding * get_keyb_of_n(struct KeyBinding * kb_p, uint16_t n)
+static struct KeyBindingDB * char_selected_kb_db(char c)
{
- uint16_t i = 0;
- while (1)
+ struct KeyBindingDB * kbdb;
+ kbdb = &world.kb_global;
+ if ('g' == c)
{
- if (n == i)
- {
- break;
- }
- i++;
- kb_p = kb_p->next;
+ kbdb = &world.kb_wingeom;
}
- return kb_p;
-}
-
-
-
-static uint16_t get_n_of_keybs(struct KeyBinding * kb_p)
-{
- uint16_t i = 0;
- while (1)
+ else if ('k' == c)
{
- if (0 == kb_p)
- {
- break;
- }
- i++;
- kb_p = kb_p->next;
+ kbdb = &world.kb_winkeys;
}
- return i;
+ else if ('w' == c)
+ {
+ struct Win * w = get_win_by_id(world.winDB.active);
+ kbdb = &w->kb;
+ }
+ return kbdb;
}
@@ -69,7 +57,8 @@ static uint8_t try_keycode(uint16_t keycode_given, char * keyname,
{
if (keycode_given == keycode_match)
{
- sprintf(keyname, keyname_match);
+ int test = sprintf(keyname, "%s", keyname_match);
+ exit_trouble(test < 0, __func__, "sprintf");
return 1;
}
return 0;
@@ -77,48 +66,33 @@ static uint8_t try_keycode(uint16_t keycode_given, char * keyname,
-extern char * get_actionname_to_keycode(struct KeyBinding * kb_p, uint16_t key)
+extern struct Command * get_command_to_keycode(struct KeyBindingDB * kbdb,
+ uint16_t keycode)
{
- while (0 != kb_p)
+ uint8_t n_kb;
+ for (n_kb = 0; n_kb < kbdb->n_of_kbs; n_kb++)
{
- if (key == kb_p->key)
+ if (keycode == kbdb->kbs[n_kb].keycode)
{
- return kb_p->name;
+ return kbdb->kbs[n_kb].command;
}
- kb_p = kb_p->next;
}
return NULL;
}
-extern uint16_t get_keycode_to_action(struct KeyBinding * kb_p, char * name)
+extern char * get_keyname_to_keycode(uint16_t keycode)
{
- while (0 != kb_p)
- {
- if (0 == strcmp(kb_p->name, name))
- {
- return kb_p->key;
- }
- kb_p = kb_p->next;
- }
- return 0;
-}
-
-
-
-extern char * get_name_to_keycode(uint16_t keycode)
-{
- char * f_name = "get_name_to_keycode()";
- char * keyname = try_malloc(15, f_name); /* FIXME: Why 15? */
+ char * keyname = try_malloc(10, __func__); /* max keyname length + 1 */
if (32 < keycode && keycode < 127)
{
- sprintf(keyname, "%c", keycode);
+ exit_trouble(sprintf(keyname, "%c", keycode) < 0, __func__, "sprintf");
}
else if (keycode >= KEY_F0 && keycode <= KEY_F(63))
{
uint16_t f = keycode - KEY_F0;
- sprintf(keyname, "F%d", f);
+ exit_trouble(sprintf(keyname, "F%d", f) < 0, __func__, "sprintf");;
}
else if ( try_keycode(keycode, keyname, 9, "TAB")
|| try_keycode(keycode, keyname, 10, "RETURN")
@@ -140,118 +114,40 @@ extern char * get_name_to_keycode(uint16_t keycode)
}
else
{
- sprintf(keyname, "(unknown)");
+ exit_trouble(sprintf(keyname, "(unknown)") < 0, __func__, "sprintf");
}
return keyname;
}
-extern void init_keybindings(char * path, struct KeyBindingDB * kbd)
-{
- char * f_name = "init_keybindings()";
- FILE * file = try_fopen(path, "r", f_name);
- uint32_t lines;
- uint32_t linemax = textfile_sizes(file, &lines);
- char command[linemax + 1];
- char * cmdptr;
- struct KeyBinding ** loc_last_ptr = &kbd->kbs;
- * loc_last_ptr = 0;
- while (try_fgets(command, linemax + 1, file, f_name))
- {
- if ('\n' == command[0] || 0 == command[0])
- {
- break;
- }
- * loc_last_ptr = try_malloc(sizeof(struct KeyBinding), f_name);
- struct KeyBinding * kb_p = * loc_last_ptr;
- kb_p->next = 0;
- kb_p->key = atoi(command);
- cmdptr = strchr(command, ' ') + 1;
- kb_p->name = try_malloc(strlen(cmdptr), f_name);
- memcpy(kb_p->name, cmdptr, strlen(cmdptr) - 1);
- kb_p->name[strlen(cmdptr) - 1] = '\0';
- loc_last_ptr = & kb_p->next;
- }
- try_fclose(file, f_name);
- kbd->edit = 0;
- kbd->select = 0;
-}
-
-
-
-extern void save_keybindings(char * path, struct KeyBindingDB * kbd)
-{
- char * f_name = "save_keybindings()";
- char path_tmp[strlen(path) + 4 + 1];
- sprintf(path_tmp, "%s_tmp", path);
- FILE * file = try_fopen(path_tmp, "w", f_name);
- uint16_t linemax = 0;
- struct KeyBinding * kb_p = kbd->kbs;
- while (0 != kb_p)
- {
- if (strlen(kb_p->name) > linemax)
- {
- linemax = strlen(kb_p->name);
- }
- kb_p = kb_p->next;
- }
- linemax = linemax + 6; /* + 6 = + 3 digits + whitespace + \n + \0 */
- char line[linemax];
- kb_p = kbd->kbs;
- while (0 != kb_p)
- {
- snprintf(line, linemax, "%d %s\n", kb_p->key, kb_p->name);
- try_fwrite(line, sizeof(char), strlen(line), file, f_name);
- kb_p = kb_p->next;
- }
- try_fclose_unlink_rename(file, path_tmp, path, f_name);
-}
-
-
-
-extern void free_keybindings(struct KeyBinding * kb_start)
-{
- if (0 == kb_start)
- {
- return;
- }
- struct KeyBinding * kb_p = kb_start->next;
- if (0 != kb_p)
- {
- free_keybindings(kb_p);
- }
- free(kb_start->name);
- free(kb_start);
-}
-
-
-
-extern void mod_selected_keyb(struct KeyBindingDB * kbd)
+extern void mod_selected_keyb(char kb_c)
{
- kbd->edit = 1;
+ struct KeyBindingDB * kbdb = char_selected_kb_db(kb_c);
+ kbdb->edit = 1;
draw_all_wins();
cbreak();
- int key = getch();
- halfdelay(world.halfdelay);
- if (key < 1000)
+ timeout(-1);
+ int keycode = getch();
+ timeout(0);
+ if (keycode < 1000)
{
- struct KeyBinding * kb_p = get_keyb_of_n(kbd->kbs, kbd->select);
- kb_p->key = key;
+ kbdb->kbs[kbdb->select].keycode = keycode;
}
- kbd->edit = 0;
+ kbdb->edit = 0;
}
-extern void move_keyb_mod_selection(struct KeyBindingDB * kbd, char dir)
+extern void move_keyb_selection(char kb_c, char dir)
{
- if ('u' == dir && kbd->select > 0)
+ struct KeyBindingDB * kbdb = char_selected_kb_db(kb_c);
+ if ('u' == dir && kbdb->select > 0)
{
- kbd->select--;
+ kbdb->select--;
}
- else if ('d' == dir && kbd->select < get_n_of_keybs(kbd->kbs) - 1)
+ else if ('d' == dir && kbdb->select < kbdb->n_of_kbs - 1)
{
- kbd->select++;
+ kbdb->select++;
}
}