1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2017 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
20 #include "screen_keydef.h"
21 #include "screen_interface.h"
22 #include "screen_status.h"
23 #include "screen_find.h"
24 #include "i18n.h"
25 #include "conf.h"
26 #include "screen.h"
27 #include "screen_utils.h"
28 #include "options.h"
29 #include "Compiler.h"
31 #include <assert.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <glib.h>
36 static struct list_window *lw;
38 static command_definition_t *cmds = NULL;
40 /** the number of commands */
41 static unsigned command_n_commands = 0;
43 /**
44 * the position of the "apply" item. It's the same as command_n_commands,
45 * because array subscripts start at 0, while numbers of items start at 1.
46 */
47 gcc_pure
48 static inline unsigned
49 command_item_apply(void)
50 {
51 return command_n_commands;
52 }
54 /** the position of the "apply and save" item */
55 gcc_pure
56 static inline unsigned
57 command_item_save(void)
58 {
59 return command_item_apply() + 1;
60 }
62 /** the number of items in the "command" view */
63 gcc_pure
64 static inline unsigned
65 command_length(void)
66 {
67 return command_item_save() + 1;
68 }
71 /**
72 * The command being edited, represented by a array subscript to @cmds, or -1,
73 * if no command is being edited
74 */
75 static int subcmd = -1;
77 /** The number of keys assigned to the current command */
78 static unsigned subcmd_n_keys = 0;
80 /** The position of the up ("[..]") item */
81 gcc_const
82 static inline unsigned
83 subcmd_item_up(void)
84 {
85 return 0;
86 }
88 /** The position of the "add a key" item */
89 gcc_pure
90 static inline unsigned
91 subcmd_item_add(void)
92 {
93 return subcmd_n_keys + 1;
94 }
96 /** The number of items in the list_window, if there's a command being edited */
97 gcc_pure
98 static inline unsigned
99 subcmd_length(void)
100 {
101 return subcmd_item_add() + 1;
102 }
104 /** Check whether a given item is a key */
105 gcc_pure
106 static inline bool
107 subcmd_item_is_key(unsigned i)
108 {
109 return (i > subcmd_item_up() && i < subcmd_item_add());
110 }
112 /**
113 * Convert an item id (as in lw->selected) into a "key id", which is an array
114 * subscript to cmds[subcmd].keys.
115 */
116 gcc_const
117 static inline unsigned
118 subcmd_item_to_key_id(unsigned i)
119 {
120 return i - 1;
121 }
124 static int
125 keybindings_changed(void)
126 {
127 command_definition_t *orginal_cmds = get_command_definitions();
128 size_t size = command_n_commands * sizeof(command_definition_t);
130 return memcmp(orginal_cmds, cmds, size);
131 }
133 static void
134 apply_keys(void)
135 {
136 if (keybindings_changed()) {
137 command_definition_t *orginal_cmds = get_command_definitions();
138 size_t size = command_n_commands * sizeof(command_definition_t);
140 memcpy(orginal_cmds, cmds, size);
141 screen_status_printf(_("You have new key bindings"));
142 } else
143 screen_status_printf(_("Keybindings unchanged."));
144 }
146 static int
147 save_keys(void)
148 {
149 char *allocated = NULL;
150 const char *filename = options.key_file;
151 if (filename == NULL) {
152 if (!check_user_conf_dir()) {
153 screen_status_printf(_("Error: Unable to create directory ~/.ncmpc - %s"),
154 strerror(errno));
155 screen_bell();
156 return -1;
157 }
159 filename = allocated = build_user_key_binding_filename();
160 }
162 FILE *f = fopen(filename, "w");
163 if (f == NULL) {
164 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
165 screen_bell();
166 g_free(allocated);
167 return -1;
168 }
170 if (write_key_bindings(f, KEYDEF_WRITE_HEADER))
171 screen_status_printf(_("Wrote %s"), filename);
172 else
173 screen_status_printf(_("Error: %s - %s"), filename, strerror(errno));
175 g_free(allocated);
176 return fclose(f);
177 }
179 /* TODO: rename to check_n_keys / subcmd_count_keys? */
180 static void
181 check_subcmd_length(void)
182 {
183 unsigned i;
185 /* this loops counts the continous valid keys at the start of the the keys
186 array, so make sure you don't have gaps */
187 for (i = 0; i < MAX_COMMAND_KEYS; i++)
188 if (cmds[subcmd].keys[i] == 0)
189 break;
190 subcmd_n_keys = i;
192 list_window_set_length(lw, subcmd_length());
193 }
195 static void
196 keydef_paint(void);
198 static void
199 keydef_repaint(void)
200 {
201 keydef_paint();
202 wrefresh(lw->w);
203 }
205 /** lw->start the last time switch_to_subcmd_mode() was called */
206 static unsigned saved_start = 0;
208 static void
209 switch_to_subcmd_mode(int cmd)
210 {
211 assert(subcmd == -1);
213 saved_start = lw->start;
215 subcmd = cmd;
216 list_window_reset(lw);
217 check_subcmd_length();
219 keydef_repaint();
220 }
222 static void
223 switch_to_command_mode(void)
224 {
225 assert(subcmd != -1);
227 list_window_set_length(lw, command_length());
228 list_window_set_cursor(lw, subcmd);
229 subcmd = -1;
231 lw->start = saved_start;
233 keydef_repaint();
234 }
236 /**
237 * Delete a key from a given command's definition
238 * @param cmd_index the command
239 * @param key_index the key (see below)
240 */
241 static void
242 delete_key(int cmd_index, int key_index)
243 {
244 /* shift the keys to close the gap that appeared */
245 int i = key_index+1;
246 while (i < MAX_COMMAND_KEYS && cmds[cmd_index].keys[i])
247 cmds[cmd_index].keys[key_index++] = cmds[cmd_index].keys[i++];
249 /* As key_index now holds the index of the last key slot that contained
250 a key, we use it to empty this slot, because this key has been copied
251 to the previous slot in the loop above */
252 cmds[cmd_index].keys[key_index] = 0;
254 cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
255 check_subcmd_length();
257 screen_status_printf(_("Deleted"));
259 /* repaint */
260 keydef_repaint();
262 /* update key conflict flags */
263 check_key_bindings(cmds, NULL, 0);
264 }
266 /* assigns a new key to a key slot */
267 static void
268 overwrite_key(int cmd_index, int key_index)
269 {
270 assert(key_index < MAX_COMMAND_KEYS);
272 char *buf = g_strdup_printf(_("Enter new key for %s: "),
273 cmds[cmd_index].name);
274 const int key = screen_getch(buf);
275 g_free(buf);
277 if (key == ERR) {
278 screen_status_printf(_("Aborted"));
279 return;
280 }
282 if (key == '\0') {
283 screen_status_printf(_("Ctrl-Space can't be used"));
284 return;
285 }
287 const command_t cmd = find_key_command(key, cmds);
288 if (cmd != CMD_NONE) {
289 screen_status_printf(_("Error: key %s is already used for %s"),
290 key2str(key), get_key_command_name(cmd));
291 screen_bell();
292 return;
293 }
295 cmds[cmd_index].keys[key_index] = key;
296 cmds[cmd_index].flags |= COMMAND_KEY_MODIFIED;
298 screen_status_printf(_("Assigned %s to %s"),
299 key2str(key),cmds[cmd_index].name);
300 check_subcmd_length();
302 /* repaint */
303 keydef_repaint();
305 /* update key conflict flags */
306 check_key_bindings(cmds, NULL, 0);
307 }
309 /* assign a new key to a new slot */
310 static void
311 add_key(int cmd_index)
312 {
313 if (subcmd_n_keys < MAX_COMMAND_KEYS)
314 overwrite_key(cmd_index, subcmd_n_keys);
315 }
317 static const char *
318 list_callback(unsigned idx, gcc_unused void *data)
319 {
320 static char buf[256];
322 if (subcmd == -1) {
323 if (idx == command_item_apply())
324 return _("===> Apply key bindings ");
325 if (idx == command_item_save())
326 return _("===> Apply & Save key bindings ");
328 assert(idx < (unsigned) command_n_commands);
330 /*
331 * Format the lines in two aligned columnes for the key name and
332 * the description, like this:
333 *
334 * this-command - do this
335 * that-one - do that
336 */
337 size_t len = strlen(cmds[idx].name);
338 strncpy(buf, cmds[idx].name, sizeof(buf));
340 if (len < get_cmds_max_name_width(cmds))
341 memset(buf + len, ' ', get_cmds_max_name_width(cmds) - len);
343 g_snprintf(buf + get_cmds_max_name_width(cmds),
344 sizeof(buf) - get_cmds_max_name_width(cmds),
345 " - %s", _(cmds[idx].description));
347 return buf;
348 } else {
349 if (idx == subcmd_item_up())
350 return "[..]";
352 if (idx == subcmd_item_add()) {
353 g_snprintf(buf, sizeof(buf), "%d. %s",
354 idx, _("Add new key"));
355 return buf;
356 }
358 assert(subcmd_item_is_key(idx));
360 g_snprintf(buf, sizeof(buf),
361 "%d. %-20s (%d) ", idx,
362 key2str(cmds[subcmd].keys[subcmd_item_to_key_id(idx)]),
363 cmds[subcmd].keys[subcmd_item_to_key_id(idx)]);
364 return buf;
365 }
366 }
368 static void
369 keydef_init(WINDOW *w, int cols, int rows)
370 {
371 lw = list_window_init(w, cols, rows);
372 }
374 static void
375 keydef_resize(int cols, int rows)
376 {
377 list_window_resize(lw, cols, rows);
378 }
380 static void
381 keydef_exit(void)
382 {
383 list_window_free(lw);
384 if (cmds)
385 g_free(cmds);
386 cmds = NULL;
387 lw = NULL;
388 }
390 static void
391 keydef_open(gcc_unused struct mpdclient *c)
392 {
393 if (cmds == NULL) {
394 command_definition_t *current_cmds = get_command_definitions();
395 command_n_commands = 0;
396 while (current_cmds[command_n_commands].name)
397 command_n_commands++;
399 /* +1 for the terminator element */
400 size_t cmds_size = (command_n_commands + 1)
401 * sizeof(command_definition_t);
402 cmds = g_malloc0(cmds_size);
403 memcpy(cmds, current_cmds, cmds_size);
404 }
406 subcmd = -1;
407 list_window_set_length(lw, command_length());
408 }
410 static void
411 keydef_close(void)
412 {
413 if (cmds && !keybindings_changed()) {
414 g_free(cmds);
415 cmds = NULL;
416 } else
417 screen_status_printf(_("Note: Did you forget to \'Apply\' your changes?"));
418 }
420 static const char *
421 keydef_title(char *str, size_t size)
422 {
423 if (subcmd == -1)
424 return _("Edit key bindings");
426 g_snprintf(str, size, _("Edit keys for %s"), cmds[subcmd].name);
427 return str;
428 }
430 static void
431 keydef_paint(void)
432 {
433 list_window_paint(lw, list_callback, NULL);
434 }
436 static bool
437 keydef_cmd(gcc_unused struct mpdclient *c, command_t cmd)
438 {
439 if (cmd == CMD_LIST_RANGE_SELECT)
440 return false;
442 if (list_window_cmd(lw, cmd)) {
443 keydef_repaint();
444 return true;
445 }
447 switch(cmd) {
448 case CMD_PLAY:
449 if (subcmd == -1) {
450 if (lw->selected == command_item_apply()) {
451 apply_keys();
452 } else if (lw->selected == command_item_save()) {
453 apply_keys();
454 save_keys();
455 } else {
456 switch_to_subcmd_mode(lw->selected);
457 }
458 } else {
459 if (lw->selected == subcmd_item_up()) {
460 switch_to_command_mode();
461 } else if (lw->selected == subcmd_item_add()) {
462 add_key(subcmd);
463 } else {
464 /* just to be sure ;-) */
465 assert(subcmd_item_is_key(lw->selected));
466 overwrite_key(subcmd, subcmd_item_to_key_id(lw->selected));
467 }
468 }
469 return true;
470 case CMD_GO_PARENT_DIRECTORY:
471 case CMD_GO_ROOT_DIRECTORY:
472 if (subcmd != -1)
473 switch_to_command_mode();
474 return true;
475 case CMD_DELETE:
476 if (subcmd != -1 && subcmd_item_is_key(lw->selected))
477 delete_key(subcmd, subcmd_item_to_key_id(lw->selected));
479 return true;
480 case CMD_ADD:
481 if (subcmd != -1)
482 add_key(subcmd);
483 return true;
484 case CMD_SAVE_PLAYLIST:
485 apply_keys();
486 save_keys();
487 return true;
488 case CMD_LIST_FIND:
489 case CMD_LIST_RFIND:
490 case CMD_LIST_FIND_NEXT:
491 case CMD_LIST_RFIND_NEXT:
492 screen_find(lw, cmd, list_callback, NULL);
493 keydef_repaint();
494 return true;
496 default:
497 return false;
498 }
500 /* unreachable */
501 assert(0);
502 return false;
503 }
505 const struct screen_functions screen_keydef = {
506 .init = keydef_init,
507 .exit = keydef_exit,
508 .open = keydef_open,
509 .close = keydef_close,
510 .resize = keydef_resize,
511 .paint = keydef_paint,
512 .cmd = keydef_cmd,
513 .get_title = keydef_title,
514 };