X-Git-Url: https://plomlompom.com/repos/index.html?a=blobdiff_plain;f=src%2Fclient%2Fkeybindings.c;h=fd62dda809b9e4ac40fe55474f5ddd5687554867;hb=024b404c0db59dc6651b1c8f3d379c2797654fdf;hp=ab34d2443faf313aad9717ed33bc7590c8d0210e;hpb=a8097b8fef09444ebac0f1e2d3ffc4e621557b28;p=plomrogue
diff --git a/src/client/keybindings.c b/src/client/keybindings.c
index ab34d24..fd62dda 100644
--- a/src/client/keybindings.c
+++ b/src/client/keybindings.c
@@ -2,15 +2,13 @@
#include "keybindings.h"
#include /* keycode defines, cbreak(), halfdelay(), getch() */
+#include /* NULL */
#include /* FILE, sprintf(), snprintf() */
#include /* uint8_t, uint16_t, uint32_t */
#include /* free(), atoi() */
-#include /* strlen(), strchr() */
-#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 /* strlen(), strchr(), strcmp() */
+#include "../common/readwrite.h" /* textfile_sizes(), try_fgets(),try_fwrite()*/
+#include "../common/try_malloc.h" /* try_malloc() */
#include "windows.h" /* draw_all_wins() */
#include "world.h" /* global world */
@@ -22,6 +20,11 @@ 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.
*/
@@ -64,6 +67,28 @@ static uint16_t get_n_of_keybs(struct KeyBinding * kb_p)
+static struct KeyBindingDB * char_selected_kb_db(char c)
+{
+ struct KeyBindingDB * kbd;
+ kbd = &world.kb_global;
+ if ('g' == c)
+ {
+ kbd = &world.kb_wingeom;
+ }
+ else if ('k' == c)
+ {
+ kbd = &world.kb_winkeys;
+ }
+ else if ('w' == c)
+ {
+ struct Win * w = get_win_by_id(world.windb.active);
+ kbd = &w->kb;
+ }
+ return kbd;
+}
+
+
+
static uint8_t try_keycode(uint16_t keycode_given, char * keyname,
uint16_t keycode_match, char * keyname_match)
{
@@ -133,44 +158,10 @@ extern char * get_keyname_to_keycode(uint16_t keycode)
-extern void init_keybindings(char * path, struct KeyBindingDB * kbd)
+extern void write_keybindings_to_file(FILE * file, struct KeyBindingDB * kbd,
+ char * delim)
{
- 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;
- cmdptr[strlen(cmdptr) - 1] = '\0';
- kb_p->command = get_command(cmdptr);
- 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);
+ char * f_name = "write_keybindings_to_file()";
uint16_t linemax = 0;
struct KeyBinding * kb_p = kbd->kbs;
while (0 != kb_p)
@@ -181,16 +172,44 @@ extern void save_keybindings(char * path, struct KeyBindingDB * kbd)
}
kb_p = kb_p->next;
}
- linemax = linemax + 6; /* + 6 = + 3 digits + whitespace + \n + \0 */
+ linemax = linemax + 6; /* + 6 = + 3 digits + ' ' + '\n' + '\0' */
char line[linemax];
kb_p = kbd->kbs;
while (0 != kb_p)
{
- snprintf(line, linemax, "%d %s\n", kb_p->key, kb_p->command->dsc_short);
+ sprintf(line, "%d %s\n", kb_p->key, kb_p->command->dsc_short);
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);
+ try_fwrite(delim, strlen(delim), 1, file, f_name);
+}
+
+
+
+extern void read_keybindings_from_file(char * line, uint32_t linemax,
+ FILE * file, struct KeyBindingDB * kbd)
+{
+ char * f_name = "read_keybindings_from_file()";
+ char * cmdptr;
+ struct KeyBinding ** loc_last_ptr = &kbd->kbs;
+ * loc_last_ptr = 0;
+ while (try_fgets(line, linemax + 1, file, f_name))
+ {
+ if (!strcmp("%\n", line))
+ {
+ 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(line);
+ cmdptr = strchr(line, ' ') + 1;
+ cmdptr[strlen(cmdptr) - 1] = '\0';
+ kb_p->command = get_command(cmdptr);
+ loc_last_ptr = & kb_p->next;
+ }
+ kbd->edit = 0;
+ kbd->select = 0;
}
@@ -211,8 +230,9 @@ extern void free_keybindings(struct KeyBinding * kb_start)
-extern void mod_selected_keyb(struct KeyBindingDB * kbd)
+extern void mod_selected_keyb(char kb_c)
{
+ struct KeyBindingDB * kbd = char_selected_kb_db(kb_c);
kbd->edit = 1;
draw_all_wins();
cbreak();
@@ -228,8 +248,9 @@ extern void mod_selected_keyb(struct KeyBindingDB * kbd)
-extern void move_keyb_mod_selection(struct KeyBindingDB * kbd, char dir)
+extern void move_keyb_selection(char kb_c, char dir)
{
+ struct KeyBindingDB * kbd = char_selected_kb_db(kb_c);
if ('u' == dir && kbd->select > 0)
{
kbd->select--;