1 /**************************************************************************
2  **
3  ** sngrep - SIP Messages flow viewer
4  **
5  ** Copyright (C) 2013-2018 Ivan Alonso (Kaian)
6  ** Copyright (C) 2013-2018 Irontec SL. All rights reserved.
7  **
8  ** This program is free software: you can redistribute it and/or modify
9  ** it under the terms of the GNU General Public License as published by
10  ** the Free Software Foundation, either version 3 of the License, or
11  ** (at your option) any later version.
12  **
13  ** This program is distributed in the hope that it will be useful,
14  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  ** GNU General Public License for more details.
17  **
18  ** You should have received a copy of the GNU General Public License
19  ** along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  **
21  ****************************************************************************/
22 /**
23  * @file option.h
24  * @author Ivan Alonso [aka Kaian] <kaian@irontec.com>
25  *
26  * @brief Functions to manage keybindings
27  *
28  * sngrep keybindings are associated with actions. Each action can store multiple
29  * keybindings.
30  * Keybindings configured by user using *key* directive of sngreprc file,
31  * in the format:
32  *
33  *   key ui_action keycode
34  *
35  * keycode must be a letter (lowercase or uppercase) or a ^ sign with an uppercase
36  * letter when Ctrl modifier is used.
37  *
38  */
39 
40 #ifndef __SNGREP_KEYBINDING_H_
41 #define __SNGREP_KEYBINDING_H_
42 
43 //! Number of keybindings per action
44 #define MAX_BINDINGS    5
45 
46 //! Some undefined key codes
47 #define KEY_CTRL(n)     ((n)-64)
48 #define KEY_ESC         27
49 #define KEY_INTRO       10
50 #define KEY_TAB         9
51 #define KEY_BACKSPACE2  8
52 #define KEY_BACKSPACE3  127
53 #define KEY_SPACE       ' '
54 
55 /**
56  * @brief Available Key actions
57  */
58 enum key_actions {
59     ACTION_PRINTABLE = 0,
60     ACTION_UP,
61     ACTION_DOWN,
62     ACTION_LEFT,
63     ACTION_RIGHT,
64     ACTION_DELETE,
65     ACTION_BACKSPACE,
66     ACTION_NPAGE,
67     ACTION_PPAGE,
68     ACTION_HNPAGE,
69     ACTION_HPPAGE,
70     ACTION_BEGIN,
71     ACTION_END,
72     ACTION_PREV_FIELD,
73     ACTION_NEXT_FIELD,
74     ACTION_RESIZE_SCREEN,
75     ACTION_CLEAR,
76     ACTION_CLEAR_CALLS,
77     ACTION_CLEAR_CALLS_SOFT,
78     ACTION_TOGGLE_SYNTAX,
79     ACTION_CYCLE_COLOR,
80     ACTION_COMPRESS,
81     ACTION_SHOW_HOSTNAMES,
82     ACTION_SHOW_ALIAS,
83     ACTION_TOGGLE_PAUSE,
84     ACTION_PREV_SCREEN,
85     ACTION_SHOW_HELP,
86     ACTION_SHOW_RAW,
87     ACTION_SHOW_FLOW,
88     ACTION_SHOW_FLOW_EX,
89     ACTION_SHOW_FILTERS,
90     ACTION_SHOW_COLUMNS,
91     ACTION_SHOW_SETTINGS,
92     ACTION_SHOW_STATS,
93     ACTION_COLUMN_MOVE_UP,
94     ACTION_COLUMN_MOVE_DOWN,
95     ACTION_SDP_INFO,
96     ACTION_DISP_FILTER,
97     ACTION_SAVE,
98     ACTION_SELECT,
99     ACTION_CONFIRM,
100     ACTION_TOGGLE_MEDIA,
101     ACTION_ONLY_MEDIA,
102     ACTION_TOGGLE_RAW,
103     ACTION_INCREASE_RAW,
104     ACTION_DECREASE_RAW,
105     ACTION_RESET_RAW,
106     ACTION_ONLY_SDP,
107     ACTION_TOGGLE_HINT,
108     ACTION_AUTOSCROLL,
109     ACTION_SORT_PREV,
110     ACTION_SORT_NEXT,
111     ACTION_SORT_SWAP,
112     ACTION_TOGGLE_TIME,
113     ACTION_SENTINEL
114 };
115 
116 //! Shorter declaration of key_binding structure
117 typedef struct key_binding key_binding_t;
118 
119 /**
120  * @brief Struct to hold a keybinding data
121  */
122 struct key_binding {
123     //! Keybinding action id
124     int id;
125     //! Keybinding action name
126     const char *name;
127     //! keybindings for this action
128     int keys[MAX_BINDINGS];
129     //! How many keys are binded to this action
130     int bindcnt;
131 };
132 
133 /**
134  * @brief Print configured keybindigs
135  */
136 void
137 key_bindings_dump();
138 
139 /**
140  * @brief Return Keybinding data for a given action
141  * @return key_binding_t structure pointer or NULL if not found
142  */
143 key_binding_t *
144 key_binding_data(int action);
145 
146 /**
147  * @brief Bind a key to an action
148  *
149  * @param action One action defined in @key_actions
150  * @param key Keycode returned by getch
151  */
152 void
153 key_bind_action(int action, int key);
154 
155 /**
156  * @brief Unbind a key to an action
157  *
158  * @param action One action defined in @key_actions
159  * @param key Keycode returned by getch
160  */
161 void
162 key_unbind_action(int action, int key);
163 
164 /**
165  * @brief Find the next action for a given key
166  *
167  * Set start parameter to -1 for start searching the
168  * first action.
169  *
170  * @param action One action defined in @key_actions
171  * @param key Keycode returned by getch
172  */
173 int
174 key_find_action(int key, int start);
175 
176 /**
177  * @brief Return the action id associate to an action str
178  *
179  * This function is used to translate keybindings configuration
180  * found in sngreprc file to internal Action IDs
181  *
182  * @param action Configuration string for an action
183  * @return action id from @key_actions or -1 if none found
184  */
185 int
186 key_action_id(const char *action);
187 
188 /**
189  * @brief Check if key is a printable ascii character
190  *
191  * @return 1 if key is alphanumeric or space
192  */
193 int
194 key_is_printable(int key);
195 
196 /**
197  * @brief Return a Human readable representation of a key
198  *
199  * @return Character string representing the key
200  */
201 const char *
202 key_to_str(int key);
203 
204 /**
205  * @brief Parse Human key declaration to curses key
206  *
207  * This function is used to translate keybindings configuration
208  * keys found in sngreprc file into internal ncurses keycodes
209  *
210  * @return ncurses keycode for the given key string
211  */
212 int
213 key_from_str(const char *key);
214 
215 /**
216  * @brief Return Human readable key for an action
217  *
218  * This function is used to display keybindings in the bottom bar
219  * of panels. Depending on sngrep configuration it will display the
220  * first associated keybding with the action or the second one
221  * (aka alternative).
222  *
223  * @param action One action defined in @key_actions
224  * @return Main/Alt keybinding for the given action
225  */
226 const char *
227 key_action_key_str(int action);
228 
229 /**
230  * @brief Return key value for a given action
231  *
232  * @param action One action defined in @key_actions
233  * @return Main/Alt keybinding for the given action
234  */
235 int
236 key_action_key(int action);
237 
238 #endif /* __SNGREP_KEYBINDING_H_ */
239