Split file src/keybind.[ch] to lib/keybind.[ch] and src/keybind-defaults.[ch].
[midnight-commander.git] / src / learn.c
blobd3febef7c9ce341522cf61ba05d16d4f553bb5ba
1 /* Learn keys
2 Copyright (C) 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2007, 2009 Free Software Foundation, Inc.
5 Written by: 1995 Jakub Jelinek
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 /** \file learn.c
23 * \brief Source: learn keys module
26 #include <config.h>
28 #include <ctype.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
37 #include "lib/global.h"
39 #include "lib/tty/tty.h"
40 #include "lib/tty/key.h"
41 #include "lib/mcconfig.h" /* Save profile */
42 #include "lib/strescape.h"
43 #include "lib/strutil.h"
44 #include "lib/util.h" /* convert_controls() */
45 #include "lib/widget.h"
47 #include "setup.h"
48 #include "layout.h" /* repaint_screen() */
49 #include "learn.h"
51 /*** global variables ****************************************************************************/
53 /*** file scope macro definitions ****************************************************************/
55 #define UX 4
56 #define UY 3
58 #define BY UY + 17
60 #define ROWS 13
61 #define COLSHIFT 23
63 #define BUTTONS 2
65 /*** file scope type declarations ****************************************************************/
67 typedef struct
69 Widget *button;
70 Widget *label;
71 int ok;
72 char *sequence;
73 } learnkey;
75 /*** file scope variables ************************************************************************/
77 static struct
79 int ret_cmd, flags, y, x;
80 const char *text;
81 } learn_but[BUTTONS] =
83 /* *INDENT-OFF */
85 B_CANCEL, NORMAL_BUTTON, 0, 39, N_("&Cancel")},
87 B_ENTER, DEFPUSH_BUTTON, 0, 25, N_("&Save")}
88 /* *INDENT-ON */
91 static Dlg_head *learn_dlg;
93 static learnkey *learnkeys = NULL;
94 static int learn_total;
95 static int learnok;
96 static int learnchanged;
97 static const char *learn_title = N_("Learn keys");
99 /*** file scope functions ************************************************************************/
100 /* --------------------------------------------------------------------------------------------- */
102 static int
103 learn_button (WButton * button, int action)
105 Dlg_head *d;
106 char *seq;
108 (void) button;
110 d = create_message (D_ERROR, _("Teach me a key"),
111 _("Please press the %s\n"
112 "and then wait until this message disappears.\n\n"
113 "Then, press it again to see if OK appears\n"
114 "next to its button.\n\n"
115 "If you want to escape, press a single Escape key\n"
116 "and wait as well."), _(key_name_conv_tab[action - B_USER].longname));
117 mc_refresh ();
118 if (learnkeys[action - B_USER].sequence != NULL)
120 g_free (learnkeys[action - B_USER].sequence);
121 learnkeys[action - B_USER].sequence = NULL;
123 seq = learn_key ();
125 if (seq)
127 /* Esc hides the dialog and do not allow definitions of
128 * regular characters
130 gboolean seq_ok = FALSE;
132 if (*seq && strcmp (seq, "\\e") && strcmp (seq, "\\e\\e")
133 && strcmp (seq, "^m") && strcmp (seq, "^i") && (seq[1] || (*seq < ' ' || *seq > '~')))
136 learnchanged = 1;
137 learnkeys[action - B_USER].sequence = seq;
138 seq = convert_controls (seq);
139 seq_ok = define_sequence (key_name_conv_tab[action - B_USER].code, seq, MCKEY_NOACTION);
142 if (!seq_ok)
143 message (D_NORMAL, _("Cannot accept this key"), _("You have entered \"%s\""), seq);
145 g_free (seq);
148 dlg_run_done (d);
149 destroy_dlg (d);
150 dlg_select_widget (learnkeys[action - B_USER].button);
151 return 0; /* Do not kill learn_dlg */
154 /* --------------------------------------------------------------------------------------------- */
156 static int
157 learn_move (int right)
159 int i, totalcols;
161 totalcols = (learn_total - 1) / ROWS + 1;
162 for (i = 0; i < learn_total; i++)
163 if (learnkeys[i].button == (Widget *) learn_dlg->current->data)
165 if (right)
167 if (i < learn_total - ROWS)
168 i += ROWS;
169 else
170 i %= ROWS;
172 else
174 if (i / ROWS)
175 i -= ROWS;
176 else if (i + (totalcols - 1) * ROWS >= learn_total)
177 i += (totalcols - 2) * ROWS;
178 else
179 i += (totalcols - 1) * ROWS;
181 dlg_select_widget (learnkeys[i].button);
182 return 1;
184 return 0;
187 /* --------------------------------------------------------------------------------------------- */
189 static int
190 learn_check_key (int c)
192 int i;
194 for (i = 0; i < learn_total; i++)
196 if (key_name_conv_tab[i].code != c || learnkeys[i].ok)
197 continue;
199 dlg_select_widget (learnkeys[i].button);
200 /* TRANSLATORS: This label appears near learned keys. Keep it short. */
201 label_set_text ((WLabel *) learnkeys[i].label, _("OK"));
202 learnkeys[i].ok = 1;
203 learnok++;
204 if (learnok >= learn_total)
206 learn_dlg->ret_value = B_CANCEL;
207 if (learnchanged)
209 if (query_dialog (learn_title,
211 ("It seems that all your keys already\n"
212 "work fine. That's great."), D_ERROR, 2,
213 _("&Save"), _("&Discard")) == 0)
214 learn_dlg->ret_value = B_ENTER;
216 else
218 message (D_ERROR, learn_title,
220 ("Great! You have a complete terminal database!\n"
221 "All your keys work well."));
223 dlg_stop (learn_dlg);
225 return 1;
227 switch (c)
229 case KEY_LEFT:
230 case 'h':
231 return learn_move (0);
232 case KEY_RIGHT:
233 case 'l':
234 return learn_move (1);
235 case 'j':
236 dlg_one_down (learn_dlg);
237 return 1;
238 case 'k':
239 dlg_one_up (learn_dlg);
240 return 1;
243 /* Prevent from disappearing if a non-defined sequence is pressed
244 and contains a button hotkey. Only recognize hotkeys with ALT. */
245 if (c < 255 && g_ascii_isalnum (c))
246 return 1;
248 return 0;
251 /* --------------------------------------------------------------------------------------------- */
253 static cb_ret_t
254 learn_callback (Dlg_head * h, Widget * sender, dlg_msg_t msg, int parm, void *data)
256 switch (msg)
258 case DLG_DRAW:
259 common_dialog_repaint (h);
260 return MSG_HANDLED;
262 case DLG_KEY:
263 return learn_check_key (parm);
265 default:
266 return default_dlg_callback (h, sender, msg, parm, data);
270 /* --------------------------------------------------------------------------------------------- */
272 static void
273 init_learn (void)
275 int x, y, i, j;
276 const key_code_name_t *key;
277 char buffer[BUF_TINY];
279 #ifdef ENABLE_NLS
280 static int i18n_flag = 0;
281 if (!i18n_flag)
283 learn_but[0].text = _(learn_but[0].text);
284 learn_but[0].x = 78 / 2 + 4;
286 learn_but[1].text = _(learn_but[1].text);
287 learn_but[1].x = 78 / 2 - (str_term_width1 (learn_but[1].text) + 9);
289 learn_title = _(learn_title);
290 i18n_flag = 1;
292 #endif /* ENABLE_NLS */
294 do_refresh ();
296 learn_dlg =
297 create_dlg (TRUE, 0, 0, 23, 78, dialog_colors, learn_callback,
298 "[Learn keys]", learn_title, DLG_CENTER | DLG_REVERSE);
300 for (i = 0; i < BUTTONS; i++)
301 add_widget (learn_dlg,
302 button_new (BY + learn_but[i].y, learn_but[i].x,
303 learn_but[i].ret_cmd, learn_but[i].flags, _(learn_but[i].text), 0));
305 x = UX;
306 y = UY;
307 for (key = key_name_conv_tab, j = 0;
308 key->name != NULL && strcmp (key->name, "kpleft"); key++, j++)
310 learnkeys = g_new (learnkey, j);
311 x += ((j - 1) / ROWS) * COLSHIFT;
312 y += (j - 1) % ROWS;
313 learn_total = j;
314 learnok = 0;
315 learnchanged = 0;
316 for (i = j - 1, key = key_name_conv_tab + j - 1; i >= 0; i--, key--)
318 learnkeys[i].ok = 0;
319 learnkeys[i].sequence = NULL;
320 g_snprintf (buffer, sizeof (buffer), "%-16s", _(key->longname));
321 add_widget (learn_dlg, learnkeys[i].button = (Widget *)
322 button_new (y, x, B_USER + i, NARROW_BUTTON, buffer, learn_button));
323 add_widget (learn_dlg, learnkeys[i].label = (Widget *) label_new (y, x + 19, ""));
324 if (i % 13)
325 y--;
326 else
328 x -= COLSHIFT;
329 y = UY + ROWS - 1;
332 add_widget (learn_dlg,
333 label_new (UY + 14, 5,
334 _("Press all the keys mentioned here. After you have done it, check")));
335 add_widget (learn_dlg,
336 label_new (UY + 15, 5,
337 _("which keys are not marked with OK. Press space on the missing")));
338 add_widget (learn_dlg,
339 label_new (UY + 16, 5,
340 _("key, or click with the mouse to define it. Move around with Tab.")));
343 /* --------------------------------------------------------------------------------------------- */
345 static void
346 learn_done (void)
348 destroy_dlg (learn_dlg);
349 repaint_screen ();
352 /* --------------------------------------------------------------------------------------------- */
354 static void
355 learn_save (void)
357 int i;
358 int profile_changed = 0;
359 char *section = g_strconcat ("terminal:", getenv ("TERM"), (char *) NULL);
360 char *esc_str;
362 for (i = 0; i < learn_total; i++)
364 if (learnkeys[i].sequence != NULL)
366 profile_changed = 1;
368 esc_str = strutils_escape (learnkeys[i].sequence, -1, ";\\", TRUE);
370 mc_config_set_string_raw (mc_main_config, section,
371 key_name_conv_tab[i].name, esc_str);
373 g_free (esc_str);
377 /* On the one hand no good idea to save the complete setup but
378 * without 'Auto save setup' the new key-definitions will not be
379 * saved unless the user does an 'Options/Save Setup'.
380 * On the other hand a save-button that does not save anything to
381 * disk is much worse.
383 if (profile_changed)
384 mc_config_save_file (mc_main_config, NULL);
386 g_free (section);
389 /* --------------------------------------------------------------------------------------------- */
390 /*** public functions ****************************************************************************/
391 /* --------------------------------------------------------------------------------------------- */
393 void
394 learn_keys (void)
396 int save_old_esc_mode = old_esc_mode;
397 int save_alternate_plus_minus = alternate_plus_minus;
399 old_esc_mode = 0; /* old_esc_mode cannot work in learn keys dialog */
400 alternate_plus_minus = 1; /* don't translate KP_ADD, KP_SUBTRACT and
401 KP_MULTIPLY to '+', '-' and '*' in
402 correct_key_code */
403 application_keypad_mode ();
404 init_learn ();
406 run_dlg (learn_dlg);
408 old_esc_mode = save_old_esc_mode;
409 alternate_plus_minus = save_alternate_plus_minus;
411 if (!alternate_plus_minus)
412 numeric_keypad_mode ();
414 switch (learn_dlg->ret_value)
416 case B_ENTER:
417 learn_save ();
418 break;
421 learn_done ();
424 /* --------------------------------------------------------------------------------------------- */