+int get_action_key (struct KeyBinding * keybindings, char * name) {
+// Return key matching name in keybindings.
+ int i = 0;
+ while (strcmp(keybindings[i].name, name) )
+ i++;
+ return keybindings[i].key; }
+
+void draw_keys_window (struct Win * win) {
+// Draw keybinding window.
+ struct World * world = (struct World *) win->data;
+ struct KeysWinData * keyswindata = (struct KeysWinData *) world->keyswindata;
+ struct KeyBinding * keybindings = world->keybindings;
+ int height_av = win->height - 1;
+ int width_av = win->width - 1;
+ int offset = 0;
+ if (keyswindata->max >= height_av) {
+ if (keyswindata->select > height_av / 2)
+ if (keyswindata->select < (keyswindata->max - (height_av / 2)))
+ offset = keyswindata->select - (height_av / 2);
+ else
+ offset = keyswindata->max - height_av + 1; }
+ int keydescwidth = 3;
+ char * keydesc = malloc(keydescwidth + 1);
+ attr_t attri;
+ int y, x;
+ for (y = 0; 0 != keybindings[offset + y].name && y < height_av; y++) {
+ attri = 0;
+ if (y == keyswindata->select - offset) {
+ attri = A_REVERSE;
+ if (1 == keyswindata->edit)
+ attri = attri | A_BLINK; }
+ snprintf(keydesc, keydescwidth + 1, "%3d ", keybindings[y + offset].key);
+ for (x = 0; x < width_av; x++)
+ if (strlen(keydesc) > x)
+ mvwaddch(win->curses_win, y + 1, x + win->border_left, keydesc[x] | attri);
+ else if (strlen(keydesc) < x && x < strlen(keybindings[y + offset].name) + strlen(keydesc) + 1)
+ mvwaddch(win->curses_win, y + 1, x + win->border_left, keybindings[y + offset].name[x - strlen(keydesc) - 1] | attri);
+ else
+ mvwaddch(win->curses_win, y + 1, x + win->border_left, ' ' | attri); }
+ free(keydesc);
+ if (y < height_av)
+ for (; y < height_av; y++)
+ for (x = 0; x < width_av; x++)
+ mvwaddch(win->curses_win, y + 1, x + win->border_left, ' '); }
+
+void init_keybindings(struct World * world) {
+// Initialize keybindings from file "keybindings".
+ FILE * file = fopen("keybindings", "r");
+ int lines = 0;
+ int c = 0;
+ while (EOF != c) {
+ c = getc(file);
+ if ('\n' == c)
+ lines++; }
+ struct KeyBinding * keybindings = malloc(lines * sizeof(struct KeyBinding));
+ fseek(file, 0, SEEK_SET);
+ char * command = malloc(100);
+ char commcount = 0;
+ char * digits = malloc(10);
+ char digicount = 0;
+ int key, digimax;
+ int keycount = 0;
+ c = getc(file);
+ while (EOF != c) {
+ if ('\n' == c) {
+ keybindings[keycount].name = calloc(commcount, sizeof(char));
+ memcpy(keybindings[keycount].name, command, commcount);
+ keybindings[keycount].key = key;
+ keycount++;
+ digicount = 0;
+ commcount = 0; }
+ else if (-1 != digicount)
+ if (' ' == c) {
+ key = 0;
+ digimax = digicount - 1;
+ while (digicount > 0) {
+ digicount--;
+ key = key + ((digits[digicount] - 48) * pow(10, digimax - digicount)); }
+ digicount = -1; }
+ else {
+ digits[digicount] = c;
+ digicount++; }
+ else {
+ command[commcount] = c;
+ commcount++; }
+ c = getc(file); }
+ free(command);
+ free(digits);
+ fclose(file);
+ struct KeysWinData * keyswindata = malloc(sizeof(struct KeysWinData));
+ keyswindata->max = lines - 1;
+ keyswindata->select = 0;
+ keyswindata->edit = 0;
+ world->keybindings = keybindings;
+ world->keyswindata = keyswindata; }
+