summaryrefslogtreecommitdiff
path: root/src/bin/keyin.c
diff options
context:
space:
mode:
authorBoris Faure <billiob@gmail.com>2014-08-24 17:53:18 +0200
committerBoris Faure <billiob@gmail.com>2014-09-02 20:42:45 +0200
commit57b06067a88c5f98d5e3d1bd39414488fc9c2a1a (patch)
treeb65925af42588bade05cbb668a57c848b686427b /src/bin/keyin.c
parent57393188c7954cc6ed73a16311c190b5b79b215e (diff)
load key bindings from config
Diffstat (limited to 'src/bin/keyin.c')
-rw-r--r--src/bin/keyin.c193
1 files changed, 122 insertions, 71 deletions
diff --git a/src/bin/keyin.c b/src/bin/keyin.c
index a555363..5edfb28 100644
--- a/src/bin/keyin.c
+++ b/src/bin/keyin.c
@@ -18,6 +18,20 @@ struct _Keyout
18 int outlen; 18 int outlen;
19}; 19};
20 20
21typedef struct _Key_Binding Key_Binding;
22
23struct _Key_Binding
24{
25 uint16_t ctrl : 1;
26 uint16_t alt : 1;
27 uint16_t shift : 1;
28
29 uint16_t len;
30
31 Key_Binding_Cb cb;
32 const char *keyname;
33};
34
21static Eina_Hash *_key_bindings = NULL; 35static Eina_Hash *_key_bindings = NULL;
22 36
23/* {{{ Keys to TTY */ 37/* {{{ Keys to TTY */
@@ -545,13 +559,14 @@ key_binding_lookup(const Evas_Event_Key_Down *ev,
545 559
546 if (len > UINT16_MAX) return NULL; 560 if (len > UINT16_MAX) return NULL;
547 561
548 kb = alloca(sizeof(Key_Binding) + len + 1); 562 kb = alloca(sizeof(Key_Binding));
549 if (!kb) return NULL; 563 if (!kb) return NULL;
550 kb->ctrl = ctrl; 564 kb->ctrl = ctrl;
551 kb->alt = alt; 565 kb->alt = alt;
552 kb->shift = shift; 566 kb->shift = shift;
553 kb->len = len; 567 kb->len = len;
554 strncpy(kb->keyname, ev->keyname, kb->len + 1); 568 kb->keyname = alloca(sizeof(char) * len + 1);
569 strncpy((char *)kb->keyname, ev->keyname, kb->len + 1);
555 570
556 return eina_hash_find(_key_bindings, kb); 571 return eina_hash_find(_key_bindings, kb);
557} 572}
@@ -867,18 +882,65 @@ cb_scroll_down_line(Evas_Object *term)
867 return EINA_TRUE; 882 return EINA_TRUE;
868} 883}
869 884
885
886static Shortcut_Action _actions[] =
887{
888 {"one_page_up", gettext_noop("Scroll one page up"), cb_scroll_up_page},
889 {"one_page_down", gettext_noop("Scroll one page down"), cb_scroll_down_page},
890 {"one_line_up", gettext_noop("Scroll one line up"), cb_scroll_up_line},
891 {"one_line_down", gettext_noop("Scroll one line down"), cb_scroll_down_line},
892
893 {"paste_primary", gettext_noop("Paste Primary buffer (highlight)"), cb_paste_primary},
894 {"paste_clipboard", gettext_noop("Paste Clipboard buffer (ctrl+c/v)"), cb_paste_clipboard},
895 {"copy_primary", gettext_noop("Copy selection to Primary buffer"), cb_copy_primary},
896 {"copy_clipboard", gettext_noop("Copy selection to Clipboard buffer"), cb_copy_clipboard},
897
898 {"term_prev", gettext_noop("Focus to the previous terminal"), cb_term_prev},
899 {"term_next", gettext_noop("Focus to the next terminal"), cb_term_next},
900 {"split_h", gettext_noop("Split horizontally (new below)"), cb_split_h},
901 {"split_v", gettext_noop("Split vertically (new on right)"), cb_split_v},
902 {"tab_new", gettext_noop("Create a new \"tab\""), cb_tab_new},
903 {"tab_select", gettext_noop("Bring up \"tab\" switcher"), cb_tab_select},
904 {"tab_1", gettext_noop("Switch to terminal tab 1"), cb_tab_1},
905 {"tab_2", gettext_noop("Switch to terminal tab 2"), cb_tab_2},
906 {"tab_3", gettext_noop("Switch to terminal tab 3"), cb_tab_3},
907 {"tab_4", gettext_noop("Switch to terminal tab 4"), cb_tab_4},
908 {"tab_5", gettext_noop("Switch to terminal tab 5"), cb_tab_5},
909 {"tab_6", gettext_noop("Switch to terminal tab 6"), cb_tab_6},
910 {"tab_7", gettext_noop("Switch to terminal tab 7"), cb_tab_7},
911 {"tab_8", gettext_noop("Switch to terminal tab 8"), cb_tab_8},
912 {"tab_9", gettext_noop("Switch to terminal tab 9"), cb_tab_9},
913 {"tab_10", gettext_noop("Switch to terminal tab 10"), cb_tab_0},
914
915 {"increase_font_size", gettext_noop("Font size up 1"), cb_increase_font_size},
916 {"decrease_font_size", gettext_noop("Font size down 1"), cb_decrease_font_size},
917 {"big_font_size", gettext_noop("Diplay big font size"), cb_big_font_size},
918 {"reset_font_size", gettext_noop("Reset font size"), cb_reset_font_size},
919
920 {"miniview", gettext_noop("Display the history miniview"), cb_miniview},
921 {"cmd_box", gettext_noop("Display the command box"), cb_cmd_box},
922
923 {NULL, NULL, NULL}
924};
925
926const Shortcut_Action *
927shortcut_actions_get(void)
928{
929 return _actions;
930}
931
870/* }}} */ 932/* }}} */
871/* {{{ Key bindings */ 933/* {{{ Key bindings */
872 934
873static unsigned int 935static unsigned int
874key_binding_key_length(EINA_UNUSED const void *key) 936_key_binding_key_length(EINA_UNUSED const void *key)
875{ 937{
876 return 0; 938 return 0;
877} 939}
878 940
879static int 941static int
880key_binding_key_cmp(const void *key1, int key1_length, 942_key_binding_key_cmp(const void *key1, int key1_length,
881 const void *key2, int key2_length) 943 const void *key2, int key2_length)
882{ 944{
883 const Key_Binding *kb1 = key1, 945 const Key_Binding *kb1 = key1,
884 *kb2 = key2; 946 *kb2 = key2;
@@ -901,7 +963,7 @@ key_binding_key_cmp(const void *key1, int key1_length,
901} 963}
902 964
903static int 965static int
904key_binding_key_hash(const void *key, int key_length) 966_key_binding_key_hash(const void *key, int key_length)
905{ 967{
906 const Key_Binding *kb = key; 968 const Key_Binding *kb = key;
907 int hash; 969 int hash;
@@ -913,10 +975,10 @@ key_binding_key_hash(const void *key, int key_length)
913} 975}
914 976
915 977
916Key_Binding * 978static Key_Binding *
917key_binding_new(const char *keyname, 979_key_binding_new(const char *keyname,
918 Eina_Bool ctrl, Eina_Bool alt, Eina_Bool shift, 980 Eina_Bool ctrl, Eina_Bool alt, Eina_Bool shift,
919 Key_Binding_Cb cb) 981 Key_Binding_Cb cb)
920{ 982{
921 Key_Binding *kb; 983 Key_Binding *kb;
922 size_t len = strlen(keyname); 984 size_t len = strlen(keyname);
@@ -929,75 +991,64 @@ key_binding_new(const char *keyname,
929 kb->alt = alt; 991 kb->alt = alt;
930 kb->shift = shift; 992 kb->shift = shift;
931 kb->len = len; 993 kb->len = len;
932 strncpy(kb->keyname, keyname, kb->len + 1); 994 kb->keyname = eina_stringshare_add(keyname);
933 kb->cb = cb; 995 kb->cb = cb;
934 996
935 return kb; 997 return kb;
936} 998}
937 999
938int key_bindings_init(void) 1000static void
1001_key_binding_free(void *data)
1002{
1003 Key_Binding *kb = data;
1004 if (!kb) return;
1005 eina_stringshare_del(kb->keyname);
1006 free(kb);
1007}
1008
1009int key_bindings_load(Config *config)
939{ 1010{
940 Key_Binding *kb; 1011 Key_Binding *kb;
1012 Shortcut_Action *action = _actions;
1013 Config_Keys *key;
1014 Eina_List *l;
1015
1016 if (!_key_bindings)
1017 {
1018#if HAVE_GETTEXT && ENABLE_NLS
1019 while (action->action)
1020 {
1021 action->description = gettext(action->description);
1022 action++;
1023 }
1024#endif
1025 _key_bindings = eina_hash_new(_key_binding_key_length,
1026 _key_binding_key_cmp,
1027 _key_binding_key_hash,
1028 _key_binding_free,
1029 5);
1030 if (!_key_bindings) return -1;
1031 }
1032 else
1033 {
1034 eina_hash_free_buckets(_key_bindings);
1035 }
1036
1037 EINA_LIST_FOREACH(config->keys, l, key)
1038 {
1039 action = _actions;
1040 while (action->action && strcmp(action->action, key->cb))
1041 action++;
1042
1043 if (action->action)
1044 {
1045 kb = _key_binding_new(key->keyname, key->ctrl, key->alt,
1046 key->shift, action->cb);
1047 if (!kb) return -1;
1048 if (!eina_hash_direct_add(_key_bindings, kb, kb)) return -1;
1049 }
1050 }
941 1051
942 _key_bindings = eina_hash_new(key_binding_key_length,
943 key_binding_key_cmp,
944 key_binding_key_hash,
945 free,
946 5);
947 if (!_key_bindings) return -1;
948
949#define ADD_KB(Name, Ctrl, Alt, Shift, Cb) \
950 kb = key_binding_new(Name, Ctrl, Alt, Shift, Cb); \
951 if (!kb) return -1; \
952 if (!eina_hash_direct_add(_key_bindings, kb, kb)) return -1;
953
954 /* Ctrl- */
955 ADD_KB("Prior", 1, 0, 0, cb_term_prev);
956 ADD_KB("Next", 1, 0, 0, cb_term_next);
957 ADD_KB("0", 1, 0, 0, cb_tab_0);
958 ADD_KB("1", 1, 0, 0, cb_tab_1);
959 ADD_KB("2", 1, 0, 0, cb_tab_2);
960 ADD_KB("3", 1, 0, 0, cb_tab_3);
961 ADD_KB("4", 1, 0, 0, cb_tab_4);
962 ADD_KB("5", 1, 0, 0, cb_tab_5);
963 ADD_KB("6", 1, 0, 0, cb_tab_6);
964 ADD_KB("7", 1, 0, 0, cb_tab_7);
965 ADD_KB("8", 1, 0, 0, cb_tab_8);
966 ADD_KB("9", 1, 0, 0, cb_tab_9);
967
968 /* Alt- */
969 ADD_KB("Home", 0, 1, 0, cb_cmd_box);
970 ADD_KB("w", 0, 1, 0, cb_copy_primary);
971 ADD_KB("Return", 0, 1, 0, cb_paste_primary);
972
973 /* Ctrl-Shift- */
974 ADD_KB("Prior", 1, 0, 1, cb_split_h);
975 ADD_KB("Next", 1, 0, 1, cb_split_v);
976 ADD_KB("t", 1, 0, 1, cb_tab_new);
977 ADD_KB("Home", 1, 0, 1, cb_tab_select);
978 ADD_KB("c", 1, 0, 1, cb_copy_clipboard);
979 ADD_KB("v", 1, 0, 1, cb_paste_clipboard);
980 ADD_KB("h", 1, 0, 1, cb_miniview);
981 ADD_KB("Insert", 1, 0, 1, cb_paste_clipboard);
982
983 /* Ctrl-Alt- */
984 ADD_KB("equal", 1, 1, 0, cb_increase_font_size);
985 ADD_KB("minus", 1, 1, 0, cb_decrease_font_size);
986 ADD_KB("0", 1, 1, 0, cb_reset_font_size);
987 ADD_KB("9", 1, 1, 0, cb_big_font_size);
988
989 /* Shift- */
990 ADD_KB("Prior", 0, 0, 1, cb_scroll_up_page);
991 ADD_KB("Next", 0, 0, 1, cb_scroll_down_page);
992 ADD_KB("Up", 0, 0, 1, cb_scroll_up_line);
993 ADD_KB("Down", 0, 0, 1, cb_scroll_down_line);
994 ADD_KB("Insert", 0, 0, 1, cb_paste_primary);
995 ADD_KB("KP_Add", 0, 0, 1, cb_increase_font_size);
996 ADD_KB("KP_Subtract", 0, 0, 1, cb_decrease_font_size);
997 ADD_KB("KP_Multiply", 0, 0, 1, cb_reset_font_size);
998 ADD_KB("KP_Divide", 0, 0, 1, cb_copy_clipboard);
999
1000#undef ADD_KB
1001 return 0; 1052 return 0;
1002} 1053}
1003 1054