3 #include "keybindings.h"
4 #include <stdio.h> /* for FILE typedef*/
5 #include <stdlib.h> /* for free(), atoi() */
6 #include <stdint.h> /* for uint16_t */
7 #include <ncurses.h> /* for keycode defines in get_name_to_keycode() */
8 #include <string.h> /* for strchr(), strlen(), strcmp(), memcpy()*/
9 #include "windows.h" /* for draw_all_wins() and WinMeta struct */
10 #include "readwrite.h" /* for texfile_sizes(), try_fopen(), try_fclose()
11 * try_fclose_unlink_rename(), try_fwrite()
13 #include "main.h" /* for world global */
14 #include "rexit.h" /* for err_exit() */
15 #include "misc.h" /* for try_malloc() */
19 /* If "keycode_given" equals "keycode_match", copy "keyname_match" to "keyname"
20 * and return 1; otherwise return 0.
22 static uint8_t try_keycode(uint16_t keycode_given, char * keyname,
23 uint16_t keycode_match, char * keyname_match);
27 static uint8_t try_keycode(uint16_t keycode_given, char * keyname,
28 uint16_t keycode_match, char * keyname_match)
30 if (keycode_given == keycode_match)
32 sprintf(keyname, keyname_match);
40 extern uint16_t get_keycode_to_action(struct KeyBinding * kb_p, char * name)
44 if (0 == strcmp(kb_p->name, name))
55 extern char * get_name_to_keycode(uint16_t keycode)
57 char * f_name = "get_name_to_keycode()";
58 char * keyname = try_malloc(15, f_name);
59 if (32 < keycode && keycode < 127)
61 sprintf(keyname, "%c", keycode);
63 else if (keycode >= KEY_F0 && keycode <= KEY_F(63))
65 uint16_t f = keycode - KEY_F0;
66 sprintf(keyname, "F%d", f);
68 else if ( try_keycode(keycode, keyname, 9, "TAB")
69 || try_keycode(keycode, keyname, 10, "RETURN")
70 || try_keycode(keycode, keyname, 27, "ESCAPE")
71 || try_keycode(keycode, keyname, 32, "SPACE")
72 || try_keycode(keycode, keyname, KEY_UP, "UP")
73 || try_keycode(keycode, keyname, KEY_DOWN, "DOWN")
74 || try_keycode(keycode, keyname, KEY_LEFT, "LEFT")
75 || try_keycode(keycode, keyname, KEY_RIGHT, "RIGHT")
76 || try_keycode(keycode, keyname, KEY_HOME, "HOME")
77 || try_keycode(keycode, keyname, KEY_BACKSPACE, "BACKSPACE")
78 || try_keycode(keycode, keyname, KEY_DC, "DELETE")
79 || try_keycode(keycode, keyname, KEY_IC, "INSERT")
80 || try_keycode(keycode, keyname, KEY_NPAGE, "NEXT PAGE")
81 || try_keycode(keycode, keyname, KEY_PPAGE, "PREV PAGE")
82 || try_keycode(keycode, keyname, KEY_END, "END"))
88 sprintf(keyname, "(unknown)");
95 extern uint16_t get_n_of_keybs(struct KeyBinding * kb_p)
112 extern struct KeyBinding * get_keyb_of_n(struct KeyBinding * kb_p, uint16_t n)
129 extern void init_keybindings(char * path, struct KeyBiData * kbd)
131 char * f_name = "init_keybindings()";
133 FILE * file = try_fopen(path, "r", f_name);
134 uint16_t lines, linemax;
135 char * err = "textfile_sizes() in init_keybindings() returns error.";
136 exit_err(textfile_sizes(file, &linemax, &lines), err);
138 char command[linemax + 1];
140 struct KeyBinding ** loc_last_ptr = &kbd->kbs;
142 while (fgets(command, linemax + 1, file))
144 if ('\n' == command[0] || 0 == command[0])
148 * loc_last_ptr = try_malloc(sizeof(struct KeyBinding), f_name);
149 struct KeyBinding * kb_p = * loc_last_ptr;
151 kb_p->key = atoi(command);
152 cmdptr = strchr(command, ' ') + 1;
153 kb_p->name = try_malloc(strlen(cmdptr), f_name);
154 memcpy(kb_p->name, cmdptr, strlen(cmdptr) - 1);
155 kb_p->name[strlen(cmdptr) - 1] = '\0';
156 loc_last_ptr = & kb_p->next;
159 try_fclose(file, f_name);
167 extern void save_keybindings(char * path, struct KeyBiData * kbd)
169 char * f_name = "save_keybindings()";
171 char path_tmp[strlen(path) + 4 + 1];
172 sprintf(path_tmp, "%s_tmp", path);
173 FILE * file = try_fopen(path_tmp, "w", f_name);
175 uint16_t linemax = 0;
176 struct KeyBinding * kb_p = kbd->kbs;
179 if (strlen(kb_p->name) > linemax)
181 linemax = strlen(kb_p->name);
185 linemax = linemax + 6; /* + 6 = + 3 digits + whitespace + \n + \0 */
191 snprintf(line, linemax, "%d %s\n", kb_p->key, kb_p->name);
192 try_fwrite(line, sizeof(char), strlen(line), file, f_name);
196 try_fclose_unlink_rename(file, path_tmp, path, f_name);
201 extern void free_keybindings(struct KeyBinding * kb_start)
207 struct KeyBinding * kb_p = kb_start->next;
210 free_keybindings(kb_p);
212 free(kb_start->name);
218 extern void mod_selected_keyb(struct KeyBiData * kbd)
221 char * err = "Trouble with draw_all_wins() in mod_selected_keyb().";
222 exit_err(draw_all_wins(world.wmeta), err);
226 struct KeyBinding * kb_p = get_keyb_of_n(kbd->kbs, kbd->select);
234 extern void move_keyb_mod_selection(struct KeyBiData * kbd, char dir)
236 if ('u' == dir && kbd->select > 0)
240 else if ('d' == dir && kbd->select < get_n_of_keybs(kbd->kbs) - 1)