"Fossies" - the Fresh Open Source Software Archive

Member "links-1.03/kbdbind.c" (28 Dec 2007, 8109 Bytes) of archive /linux/www/links-1.03.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "kbdbind.c" see the Fossies "Dox" file reference documentation.

    1 #include "links.h"
    2 
    3 static void add_default_keybindings();
    4 
    5 struct keybinding {
    6     struct keybinding *next;
    7     struct keybinding *prev;
    8     int act;
    9     long x;
   10     long y;
   11 };
   12 
   13 static struct list_head keymaps[KM_MAX];
   14 
   15 static void add_keybinding(int km, int act, long x, long y)
   16 {
   17     struct keybinding *kb;
   18 
   19     foreach(kb, keymaps[km])
   20         if (kb->x == x && kb->y == y) {
   21             /* want at top of list */
   22             del_from_list(kb);
   23             goto add;
   24         }
   25     
   26     kb = mem_alloc(sizeof(struct keybinding));
   27     add:
   28     kb->act = act;
   29     kb->x = x;
   30     kb->y = y;
   31     add_to_list(keymaps[km], kb);
   32 }
   33 
   34 static void delete_keybinding(int km, long x, long y)
   35 {
   36     struct keybinding *kb;
   37     foreach(kb, keymaps[km]) 
   38         if (kb->x == x && kb->y == y) {
   39             del_from_list(kb);
   40             mem_free(kb);
   41             break;
   42         }
   43 }
   44 
   45 void init_keymaps()
   46 {
   47         int i;  
   48     for (i = 0; i < KM_MAX; i++) init_list(keymaps[i]);
   49     add_default_keybindings();
   50 }
   51 
   52 void free_keymaps() 
   53 {
   54     int i;
   55     for (i = 0; i < KM_MAX; i++) free_list(keymaps[i]);
   56 }
   57 
   58 int kbd_action(int kmap, struct event *ev)
   59 {
   60     struct keybinding *kb;
   61     if (ev->ev == EV_KBD)
   62         foreach(kb, keymaps[kmap]) 
   63             if (ev->x == kb->x && ev->y == kb->y) return kb->act;
   64     return -1;
   65 }
   66 
   67 /*
   68  * Config file helpers.
   69  */
   70 
   71 struct str2num {
   72     unsigned char *str;
   73     long num;
   74 };
   75 
   76 static long str2num(struct str2num *table, char *s)
   77 {
   78     struct str2num *p;
   79     for (p = table; p->str; p++) 
   80         if (!strcmp(p->str, s)) return p->num;
   81     return -1;
   82 }
   83 
   84 static int parse_keymap(unsigned char *s)
   85 {
   86     struct str2num table[] = {
   87         { "main", KM_MAIN },
   88         { "edit", KM_EDIT },
   89         { "menu", KM_MENU },
   90         { NULL, 0 }
   91     };
   92     return str2num(table, s);
   93 }
   94 
   95 static long parse_key(unsigned char *s)
   96 {
   97     struct str2num table[] = {
   98         { "Enter", KBD_ENTER },
   99         { "Backspace", KBD_BS },
  100         { "Tab", KBD_TAB },
  101         { "Escape", KBD_ESC },
  102         { "Left", KBD_LEFT },
  103         { "Right", KBD_RIGHT },
  104         { "Up", KBD_UP },
  105         { "Down", KBD_DOWN },
  106         { "Insert", KBD_INS },
  107         { "Delete", KBD_DEL },
  108         { "Home", KBD_HOME },
  109         { "End", KBD_END },
  110         { "PageUp", KBD_PAGE_UP },
  111         { "PageDown", KBD_PAGE_DOWN },
  112         { "F1", KBD_F1 },
  113         { "F2", KBD_F2 },
  114         { "F3", KBD_F3 },
  115         { "F4", KBD_F4 },
  116         { "F5", KBD_F5 },
  117         { "F6", KBD_F6 },
  118         { "F7", KBD_F7 },
  119         { "F8", KBD_F8 },
  120         { "F9", KBD_F9 },
  121         { "F10", KBD_F10 },
  122         { "F11", KBD_F11 },
  123         { "F12", KBD_F12 },
  124         { NULL, 0 }
  125     };
  126     return (strlen(s) == 1) ? *s : str2num(table, s);
  127 }
  128 
  129 static int parse_keystroke(unsigned char *s, long *x, long *y)
  130 {
  131     *y = 0;
  132     if (!strncmp(s, "Shift-", 6)) *y |= KBD_SHIFT, s += 6;
  133     else if (!strncmp(s, "Ctrl-", 5)) *y |= KBD_CTRL, s += 5;
  134     else if (!strncmp(s, "Alt-", 4)) *y |= KBD_ALT, s += 4;
  135     return ((*x = parse_key(s)) < 0) ? -1 : 0;
  136 }
  137 
  138 static int parse_act(unsigned char *s)
  139 {
  140     int i;
  141     unsigned char *table[] = {
  142         "add-bookmark",
  143         "auto-complete",
  144         "back",
  145         "backspace",
  146         "bookmark-manager",
  147         "copy-clipboard",
  148         "cut-clipboard",
  149         "delete",
  150         "document-info",
  151         "down",
  152         "download",
  153         "end",
  154         "enter",
  155         "file-menu",
  156         "find-next",
  157         "find-next-back",
  158         "goto-url",
  159         "goto-url-current",
  160         "goto-url-current-link",
  161         "header-info",
  162         "home",
  163         "kill-to-bol",
  164         "kill-to-eol",
  165         "left",
  166         "menu",
  167         "next-frame",
  168         "open-new-window",
  169         "open-link-in-new-window",
  170         "page-down",
  171         "page-up",
  172         "paste-clipboard",
  173         "previous-frame",
  174         "really-quit",
  175         "quit",
  176         "reload",
  177         "right",
  178         "scroll-down",
  179         "scroll-left",
  180         "scroll-right",
  181         "scroll-up",
  182         "search",
  183         "search-back",
  184         "toggle-display-images",
  185         "toggle-display-tables",
  186         "toggle-html-plain",
  187         "up",
  188         "view-image",
  189         "zoom-frame",
  190         NULL
  191     };
  192     for (i = 0; table[i]; i++) if (!strcmp(table[i], s)) return i;
  193     return -1;
  194 }
  195 
  196 /*
  197  * Config file readers.
  198  */
  199 
  200 /* bind KEYMAP KEYSTROKE ACTION */
  201 unsigned char *bind_rd(struct option *o, unsigned char *line)
  202 {
  203     unsigned char *err = NULL;
  204     unsigned char *ckmap;
  205     unsigned char *ckey;
  206     unsigned char *cact;
  207     int kmap;
  208     long x, y;
  209     int act;
  210 
  211     ckmap = get_token(&line);
  212     ckey = get_token(&line);
  213     cact = get_token(&line);
  214 
  215     if (!ckmap || !ckey || !cact)
  216         err = "Missing arguments"; 
  217     else if ((kmap = parse_keymap(ckmap)) < 0)
  218         err = "Unrecognised keymap";
  219     else if (parse_keystroke(ckey, &x, &y) < 0)
  220         err = "Error parsing keystroke";
  221     else if ((act = parse_act(cact)) < 0)
  222         err = "Unrecognised action"; 
  223     else 
  224         add_keybinding(kmap, act, x, y);
  225 
  226     if (cact) mem_free(cact);
  227     if (ckey) mem_free(ckey);
  228     if (ckmap) mem_free(ckmap);
  229     return err;
  230 }
  231 
  232 /* unbind KEYMAP KEYSTROKE */
  233 unsigned char *unbind_rd(struct option *o, unsigned char *line)
  234 {
  235     unsigned char *err = NULL;
  236     unsigned char *ckmap;
  237     unsigned char *ckey;
  238     int kmap;
  239     long x, y;
  240     
  241     ckmap = get_token(&line);
  242     ckey = get_token(&line);
  243     if (!ckmap)
  244         err = "Missing arguments"; 
  245     else if ((kmap = parse_keymap(ckmap)) < 0)
  246         err = "Unrecognised keymap";
  247     else if (parse_keystroke(ckey, &x, &y) < 0)
  248         err = "Error parsing keystroke";
  249     else 
  250         delete_keybinding(kmap, x, y);
  251 
  252     if (ckey) mem_free(ckey);
  253     if (ckmap) mem_free(ckmap);
  254     return err;
  255 }
  256 
  257 /*
  258  * Default keybindings.
  259  */
  260 
  261 struct default_kb {
  262     int act;
  263     long x;
  264     long y;
  265 };
  266 
  267 static struct default_kb default_main_keymap[] = {
  268     { ACT_PAGE_DOWN, KBD_PAGE_DOWN, 0 },
  269     { ACT_PAGE_DOWN, ' ', 0 },
  270     { ACT_PAGE_DOWN, 'F', KBD_CTRL },
  271     { ACT_PAGE_UP, KBD_PAGE_UP, 0 },
  272     { ACT_PAGE_UP, 'b', 0 },
  273     { ACT_PAGE_UP, 'B', 0 },
  274     { ACT_PAGE_UP, 'B', KBD_CTRL },
  275     { ACT_DOWN, KBD_DOWN, 0 },
  276     { ACT_UP, KBD_UP, 0 },
  277     { ACT_COPY_CLIPBOARD, KBD_INS, KBD_CTRL },
  278     { ACT_COPY_CLIPBOARD, 'C', KBD_CTRL },
  279     { ACT_SCROLL_UP, KBD_INS, 0 },
  280     { ACT_SCROLL_UP, 'P', KBD_CTRL },
  281     { ACT_SCROLL_DOWN, KBD_DEL, 0 },
  282     { ACT_SCROLL_DOWN, 'N', KBD_CTRL },
  283     { ACT_SCROLL_LEFT, '[', 0 },
  284     { ACT_SCROLL_RIGHT, ']', 0 },
  285     { ACT_HOME, KBD_HOME, 0 },
  286     { ACT_HOME, 'A', KBD_CTRL },
  287     { ACT_END, KBD_END, 0 },
  288     { ACT_END, 'E', KBD_CTRL },
  289     { ACT_ENTER, KBD_RIGHT, 0 },
  290     { ACT_ENTER, KBD_ENTER, 0 },
  291     { ACT_BACK, KBD_LEFT, 0 },
  292     { ACT_DOWNLOAD, 'd', 0 },
  293     { ACT_DOWNLOAD, 'D', 0 },
  294     { ACT_SEARCH, '/', 0 },
  295     { ACT_SEARCH_BACK, '?', 0 },
  296     { ACT_FIND_NEXT, 'n', 0 },
  297     { ACT_FIND_NEXT_BACK, 'N', 0 },
  298     { ACT_ZOOM_FRAME, 'f', 0 },
  299     { ACT_ZOOM_FRAME, 'F', 0 },
  300     { ACT_RELOAD, 'R', KBD_CTRL },
  301     { ACT_GOTO_URL, 'g', 0 },
  302     { ACT_GOTO_URL_CURRENT, 'G', 0 },
  303     { ACT_ADD_BOOKMARK, 'a', 0 },
  304     { ACT_ADD_BOOKMARK, 'A', 0 },
  305     { ACT_BOOKMARK_MANAGER, 's', 0 },
  306     { ACT_BOOKMARK_MANAGER, 'S', 0 },
  307     { ACT_QUIT, 'q', 0 },
  308     { ACT_REALLYQUIT, 'Q', 0 },
  309     { ACT_DOCUMENT_INFO, '=', 0 },
  310     { ACT_HEADER_INFO, '|', 0 },
  311     { ACT_TOGGLE_HTML_PLAIN, '\\', 0 },
  312     { ACT_TOGGLE_DISPLAY_IMAGES, '*', 0 },
  313     { ACT_NEXT_FRAME, KBD_TAB, 0 },
  314     { ACT_MENU, KBD_ESC, 0 },
  315     { ACT_MENU, KBD_F9, 0 },
  316     { ACT_FILE_MENU, KBD_F10, 0 },
  317     { 0, 0, 0 }
  318 };
  319 
  320 static struct default_kb default_edit_keymap[] = {
  321     { ACT_LEFT, KBD_LEFT, 0 },
  322     { ACT_RIGHT, KBD_RIGHT, 0 },
  323     { ACT_HOME, KBD_HOME, 0 },
  324     { ACT_HOME, 'A', KBD_CTRL },
  325     { ACT_UP, KBD_UP, 0 },
  326     { ACT_DOWN, KBD_DOWN, 0 },
  327     { ACT_END, KBD_END, 0 },
  328     { ACT_END, 'E', KBD_CTRL },
  329     { ACT_COPY_CLIPBOARD, KBD_INS, KBD_CTRL },
  330     { ACT_COPY_CLIPBOARD, 'C', KBD_CTRL },
  331     { ACT_CUT_CLIPBOARD, 'X', KBD_CTRL },
  332     { ACT_PASTE_CLIPBOARD, 'V', KBD_CTRL },
  333     { ACT_ENTER, KBD_ENTER, 0 },
  334     { ACT_BACKSPACE, KBD_BS, 0 },
  335     { ACT_BACKSPACE, 'H', KBD_CTRL },
  336     { ACT_DELETE, KBD_DEL, 0 },
  337     { ACT_DELETE, 'D', KBD_CTRL },
  338     { ACT_KILL_TO_BOL, 'U', KBD_CTRL },
  339     { ACT_KILL_TO_EOL, 'K', KBD_CTRL },
  340         { ACT_AUTO_COMPLETE, 'W', KBD_CTRL },
  341     { 0, 0, 0 }
  342 };
  343 
  344 static struct default_kb default_menu_keymap[] = {
  345     { ACT_LEFT, KBD_LEFT, 0 },
  346     { ACT_RIGHT, KBD_RIGHT, 0 },
  347     { ACT_HOME, KBD_HOME, 0 },
  348     { ACT_HOME, 'A', KBD_CTRL },
  349     { ACT_UP, KBD_UP, 0 },
  350     { ACT_DOWN, KBD_DOWN, 0 },
  351     { ACT_END, KBD_END, 0 },
  352     { ACT_END, 'E', KBD_CTRL },
  353     { ACT_ENTER, KBD_ENTER, 0 },
  354     { ACT_PAGE_DOWN, KBD_PAGE_DOWN, 0 },
  355     { ACT_PAGE_DOWN, 'F', KBD_CTRL },
  356     { ACT_PAGE_UP, KBD_PAGE_UP, 0 },
  357     { ACT_PAGE_UP, 'B', KBD_CTRL },
  358     { 0, 0, 0}
  359 };
  360 
  361 static void add_default_keybindings()
  362 {
  363     struct default_kb *kb;
  364     for (kb = default_main_keymap; kb->x; kb++) add_keybinding(KM_MAIN, kb->act, kb->x, kb->y);
  365         for (kb = default_edit_keymap; kb->x; kb++) add_keybinding(KM_EDIT, kb->act, kb->x, kb->y);
  366         for (kb = default_menu_keymap; kb->x; kb++) add_keybinding(KM_MENU, kb->act, kb->x, kb->y);
  367 }